xref: /kernel/linux/linux-5.10/tools/usb/usbip/src/utils.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
48c2ecf20Sopenharmony_ci *               2005-2007 Takahiro Hirofuchi
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <errno.h>
88c2ecf20Sopenharmony_ci#include <stdio.h>
98c2ecf20Sopenharmony_ci#include <string.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "usbip_common.h"
128c2ecf20Sopenharmony_ci#include "utils.h"
138c2ecf20Sopenharmony_ci#include "sysfs_utils.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciint modify_match_busid(char *busid, int add)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	char attr_name[] = "match_busid";
188c2ecf20Sopenharmony_ci	char command[SYSFS_BUS_ID_SIZE + 4];
198c2ecf20Sopenharmony_ci	char match_busid_attr_path[SYSFS_PATH_MAX];
208c2ecf20Sopenharmony_ci	int rc;
218c2ecf20Sopenharmony_ci	int cmd_size;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	snprintf(match_busid_attr_path, sizeof(match_busid_attr_path),
248c2ecf20Sopenharmony_ci		 "%s/%s/%s/%s/%s/%s", SYSFS_MNT_PATH, SYSFS_BUS_NAME,
258c2ecf20Sopenharmony_ci		 SYSFS_BUS_TYPE, SYSFS_DRIVERS_NAME, USBIP_HOST_DRV_NAME,
268c2ecf20Sopenharmony_ci		 attr_name);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	if (add)
298c2ecf20Sopenharmony_ci		cmd_size = snprintf(command, SYSFS_BUS_ID_SIZE + 4, "add %s",
308c2ecf20Sopenharmony_ci				    busid);
318c2ecf20Sopenharmony_ci	else
328c2ecf20Sopenharmony_ci		cmd_size = snprintf(command, SYSFS_BUS_ID_SIZE + 4, "del %s",
338c2ecf20Sopenharmony_ci				    busid);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	rc = write_sysfs_attribute(match_busid_attr_path, command,
368c2ecf20Sopenharmony_ci				   cmd_size);
378c2ecf20Sopenharmony_ci	if (rc < 0) {
388c2ecf20Sopenharmony_ci		dbg("failed to write match_busid: %s", strerror(errno));
398c2ecf20Sopenharmony_ci		return -1;
408c2ecf20Sopenharmony_ci	}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	return 0;
438c2ecf20Sopenharmony_ci}
44