tools/usb/usbip/src/usbip_attach.c
Source file repositories/reference/linux-study-clean/tools/usb/usbip/src/usbip_attach.c
File Facts
- System
- Linux kernel
- Corpus path
tools/usb/usbip/src/usbip_attach.c- Extension
.c- Size
- 4824 bytes
- Lines
- 244
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sys/stat.hlimits.hstdint.hstdio.hstring.hfcntl.hgetopt.hunistd.herrno.hvhci_driver.husbip_common.husbip_network.husbip.h
Detected Declarations
function usbip_attach_usagefunction record_connectionfunction import_devicefunction query_import_devicefunction attach_devicefunction usbip_attach
Annotated Snippet
if (errno == EEXIST) {
struct stat s;
ret = stat(VHCI_STATE_PATH, &s);
if (ret < 0)
return -1;
if (!(s.st_mode & S_IFDIR))
return -1;
} else
return -1;
}
snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport);
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
if (fd < 0)
return -1;
snprintf(buff, MAX_BUFF, "%s %s %s\n",
host, port, busid);
ret = write(fd, buff, strlen(buff));
if (ret != (ssize_t) strlen(buff)) {
close(fd);
return -1;
}
close(fd);
return 0;
}
static int import_device(int sockfd, struct usbip_usb_device *udev)
{
int rc;
int port;
uint32_t speed = udev->speed;
rc = usbip_vhci_driver_open();
if (rc < 0) {
err("open vhci_driver (is vhci_hcd loaded?)");
goto err_out;
}
do {
port = usbip_vhci_get_free_port(speed);
if (port < 0) {
err("no free port");
goto err_driver_close;
}
dbg("got free port %d", port);
rc = usbip_vhci_attach_device(port, sockfd, udev->busnum,
udev->devnum, udev->speed);
if (rc < 0 && errno != EBUSY) {
err("import device");
goto err_driver_close;
}
} while (rc < 0);
usbip_vhci_driver_close();
return port;
err_driver_close:
usbip_vhci_driver_close();
err_out:
return -1;
}
static int query_import_device(int sockfd, char *busid)
{
int rc;
struct op_import_request request;
struct op_import_reply reply;
uint16_t code = OP_REP_IMPORT;
int status;
memset(&request, 0, sizeof(request));
memset(&reply, 0, sizeof(reply));
/* send a request */
rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0);
if (rc < 0) {
err("send op_common");
return -1;
}
strncpy(request.busid, busid, SYSFS_BUS_ID_SIZE-1);
Annotation
- Immediate include surface: `sys/stat.h`, `limits.h`, `stdint.h`, `stdio.h`, `string.h`, `fcntl.h`, `getopt.h`, `unistd.h`.
- Detected declarations: `function usbip_attach_usage`, `function record_connection`, `function import_device`, `function query_import_device`, `function attach_device`, `function usbip_attach`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.