utils.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
  4. * 2005-2007 Takahiro Hirofuchi
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "usbip_common.h"
  10. #include "utils.h"
  11. #include "sysfs_utils.h"
  12. int modify_match_busid(char *busid, int add)
  13. {
  14. char attr_name[] = "match_busid";
  15. char command[SYSFS_BUS_ID_SIZE + 4];
  16. char match_busid_attr_path[SYSFS_PATH_MAX];
  17. int rc;
  18. int cmd_size;
  19. snprintf(match_busid_attr_path, sizeof(match_busid_attr_path),
  20. "%s/%s/%s/%s/%s/%s", SYSFS_MNT_PATH, SYSFS_BUS_NAME,
  21. SYSFS_BUS_TYPE, SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME,
  22. attr_name);
  23. if (add)
  24. cmd_size = snprintf(command, SYSFS_BUS_ID_SIZE + 4, "add %s",
  25. busid);
  26. else
  27. cmd_size = snprintf(command, SYSFS_BUS_ID_SIZE + 4, "del %s",
  28. busid);
  29. rc = write_sysfs_attribute(match_busid_attr_path, command,
  30. cmd_size);
  31. if (rc < 0) {
  32. dbg("failed to write match_busid: %s", strerror(errno));
  33. return -1;
  34. }
  35. return 0;
  36. }