Seeing the write() function failing on /sys/bus/pci/devices/.../driver/remove_id file returning -1 with errno equal to 19 (ENODEV).
But, same works fine via command line. I have checked the file permission which seems to be fine (--w-------) for user to perform write on this file.
int fp = 0;
int buffer_length = 0;
int bytes_written = 0;
fp = open(cmd_buf, O_WRONLY); // where cmd_buf will hold this string
// "/sys/bus/pci/devices/.../driver/remove_id"
if (fp == -1)
{
return -1;
}
// where inbuf will be a char * pointing to pci vendor device id like
// this, "XXXX YYYY"
bytes_written = write(fp, in_buf, sizeof(in_buf));
printf(" bytes_written : %d \n ", bytes_written);
Seeing bytes_written equal to -1 and errno shows 19.
Please let me know if you find something wrong with the code snippet?
You do not provide enough information to pinpoint the problem.
However, here is an example program, example.c, that shows that it is your implementation that has the bug:
Compile it using e.g.
gcc -Wall -Wextra -O2 example.c -o example
, and run using e.g../example /sys/bus/pci/devices/.../driver/remove_id "vendor_id device_id"
. Run it without arguments, or with-h
or--help
as the only argument, and it will print usage information to standard error.The program essentially does what
echo -n "vendor_id device_id" > /sys/bus/pci/devices/.../drivers/remove_id
does.If it is successful, it will not output anything, just return success (exit status 0). If there is any kind of an error, it will report it to standard error.
If you know the target path is always a device or a pseudo-file (like those in /sys or /proc), use
fd = open(path, O_WRONLY | O_NOCTTY | O_CLOEXEC);
instead.O_CLOEXEC
means that if the process forks at any point, this particular file descriptor is not copied to the child process.O_NOCTTY
means that if the path is a tty device, and the current process does not have a controlling terminal, the kernel is NOT to make the opened device the controlling terminal.echo -n
usesO_CREAT | O_TRUNC
, so that if the target path exists and is a normal file, it is truncated, but if it does not exist, it is created. It does not affect opening existing character devices and pseudofiles. WheneverO_CREAT
is used, there must be a third parameter, which affects the access mode of the file created. This mode is usually0666
, allowing read and write access as moderated by the current umask. One can obtain the current umask usingmode_t mask = umask(0); umask(mask);
. Access mode bits set in umask are always zero in the final access mode, and access mode bits clear in umask are taken from the third parameter of the open() command when the file is created.