Open file in kthread on behalf of a user process - multithreading

I am writing a linux kernel module which would start a kthread when a user process calls to it (using ioctl).
How can i open a file using this kthread on bahalf of user process, that is, when it returns the user process can access this file itself!?

It's not really sensible to do this. To open a file that the userspace process can read, you need to return a file descriptor to that process.
Potentially you could return a UNIX-domain socketpair connecting the kernel thread to the userspace thread, and have the kernel thread pass open file descriptors across that socket using a SCM_RIGHTS message.
It is likely to be more appropriate, however, to simply open the file in the context of the original process in the ioctl() call and return the file descriptor there.

Related

What can I assume about pthread_create and file descriptors?

I just debugged a program that did roughly:
pthread_create(...);
close(0);
int t = open("/named_pipe");
assert(t == 0);
Occasionally it fails, as pthread_create actually briefly opens file descriptors on the new thread – specifically /sys/devices/system/cpu/online – which if you're unlucky occur between the close and open above, making t something other than 0.
What's the safest way to do this? What if anything is guaranteed about pthread_create regarding file descriptors? Am I guaranteed that if there are 3 file descriptors open before I call pthread_create, then there'll also be 3 open when it's returned and control has been passed to my function on the new thread?
In multi-threaded programs, you need to use dup2 or dup3 to replace file descriptors. The old trick with immediate reuse after close no longer works because other threads and create file descriptors at any time. Such file descriptors can even be created (and closed) implicitly by glibc because many kernel interfaces use file descriptors.
dup2 is the standard interface. Linux also has dup3, with which you can atomically create the file descriptor with the O_CLOEXEC flag set. Otherwise, there would still be a race condition, and the descriptor could leak to a subprocess if the process ever forks and executes a new program.

Sending signalfd to another process

Process A sends a signalfd to process B. What will happen when B attempts to read()? If B adds the signalfd to an epoll, when will epoll_wait return?
There is a clue in the man page:
fork(2) semantics
After a fork(2), the child inherits a copy of the signalfd file descriptor. A read(2) from the file descriptor in the child will return information about signals queued to the child.
signalfds transferred via unix socket should behave the same as those inherited by fork(). Basically, it's irrelevant which process created the signalfd; read()ing from it always returns signals queued to the process that calls read().
There is a weird interaction with epoll, however: Since the epoll event queue is managed outside the context of any particular process, it decides the readiness of the signalfd based on the process which originally called epoll_ctl() to register interest in the signalfd. So if you arrange to watch a signalfd with an epoll FD, and then send both FDs to another process, the receiving process will see inconsistent results: epoll will signal readiness only when the sending process has a signal, but signalfd will return signals for the receiving process.
This situation is particularly easy to get into using fork(). For example, if you initialize an event loop library than uses epoll and signalfd, then call fork() (e.g. to daemonize the process), then try to use the library in the child process, you may find you cannot receive signals. (I spent all day yesterday trying to debug such a problem.)
This is inconsistent, or at least an under-documented corner-case. Read carefully signal(7).
A process A could send a signal (not a signalfd) using kill(2) or killpg(2) to a process B.
The process B is handling a signal (and there are some default behavior to handle some signals). It could install (in a POSIX-ly standardized way) a signal handler using old signal(2) or newer sigaction(2), or it could ask (in a Linux specific way) by using signalfd(2) to get some data on a file descriptor.
So signalfd gives on success a fresh file descriptor, like open or socket do.
Read the signalfd(2) documentation, it explains what is happening on B side when it reads (the kernel is sending some struct signalfd_siginfo, I imagine from the point of view of the process getting the signal, not of the process reading the file descriptor, see kernel's source file fs/signalfd.c), or waits with poll or epoll on the file descriptor given by signalfd ; the polling will succeed when a signal has been received by B.
A successful signalfd is just getting an opened file descriptor (like the file descriptors open, socket, accept, pipe are giving you) and you won't share that file descriptor with unrelated processes.
I won't make any supposition on what happens if you dare sending that file descriptor using sendmsg(2) on a unix(7) socket using SCM_RIGHTS to some other process. I guess it would be similar to pipe(7)-s or fifo(7)-s or netlink(7)-s. But I certainly won't do that: signalfd is Linux specific, and you are in an undocumented corner-case situation. Read the kernel source code to understand what is happening, or ask on kernelnewbies. And don't expect too much future kernels to behave consistently with present ones on that undocumented aspect ...

process that don't have file descriptor

when I do
ls proc/[pid]/fd
sometimes I don't get output, It seems that there are no file descriptor in that file.
What does that mean when a process doesn't have file descriptor?
The process in question is more likely a deamon — daemon processed will intentionally close standard file descriptors in order to avoid hanging onto their resources. (They will also chdir to the root directory, invoke an extra fork() and perform a number of more obscure operations for the same reason.)

Call function in running Linux process using gdb?

I have a running Linux process that is stuck on poll(). It has some data in a buffer, but this buffer is not yet written to disk. Ordinarily I'd kill the process which would cause it to flush the buffer and and exit.
However, in this case the file it's writing to has been deleted from the file system, so I need the process to write the buffer before it exits, while the inode is still reachable via /proc//fd/
Is it possible to "kick" the process out of the poll() call and single step it until it has flushed the buffer to disk using GDB?
(For the curious, the source code is here: http://sourcecodebrowser.com/alsa-utils/1.0.15/arecordmidi_8c_source.html)

Linux - Disabing buffered I/O to file in the child processes

In my application I am creating a bunch of child processes. After fork() I open a per process file, set the stdout/stderr of the created process to point to that file and then exec the intended program.
Is there an option for the parent process to setup things such a way that when the child process does a printf it gets flushed immediately to the output file without having to call flush() ? Or is there an API that can be called from the child process itself (before exec) to disable buffered I/O ?
The problem here is that printf is buffered. The underlying file descriptors are not buffered in that way (they are buffered in the kernel, but the other end can read from the same kernel buffer). You can change the buffering using setvbuf as mentioned in a comment which should have been an answer.
setvbuf(stdout, NULL, _IONBF, 0);
You do not need to do this for stdin or stderr.
You can't do this from the parent process. This is because the buffers are created by the child process. The parent process can only manipulate the underlying file descriptors (which are in the kernel), not stdout (which is part of the C library).
P.S. You mean fflush, not flush.

Resources