简体   繁体   中英

linux prevent file descriptor from closing on program exit

I have a peculiar use case where on linux when using uinput http://thiemonge.org/getting-started-with-uinput , the process that creates the virtual input device, if it dies by default releases all open file descriptors.

In this case it also releases the created virtual input device and the device flat out disappears from /dev/input.

I am wondering if there is a simple solution to this problem, the most obvious being to not release the open file descriptor upon program termination. The more annoying one to spawn a proxy process to simply hold the FD.

I ended up going the proxy approach like so:

void main(int argc, char **argv) {
   create_uinput_device();
   print_eventn();
   set_argv0_eventn();
   if (fork()) {
       return;
   } else {
       //hold the uinput fd
       while(1) { sleep(1000); }
   }
}

This way when we cat /proc/[p]/cmdline we can easily find the /dev/input/event[n] and which pid is currently holding it. We can memcpy the new cmdline to argv0. So this is kind of a hack around this.

Also conveniently when we run this program we return right away with the /dev/input/event[n] device we need to forward to qemu; due to the print.

To truly fix this someone needs to merge into qemu changes so qemu itself creates the virtual input device, this is quite complex due to the vast amount of options that can be passed. Regardless once figured out, the virtual input device created by uinput will live as long as the qemu instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM