Skip to content

redirect stdin of a program to a fifo - designed for start-stop-daemon

License

Notifications You must be signed in to change notification settings

StefaBa/fifo-to-stdin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

fifo-to-stdin

Use at your own risk

This program has been writen with no harms in mind, however:
no guarantee what-so-ever that this program actually does what it has been specified to
i am not responsible for any damage to you and/or your system and/or the persons using your system, resulting from the usage of this program
you have been warned.



program designed as a helper function for the start-stop-daemon.

the start-stop-daemon cannot redirect the stdin of the daemonized process. this program redirects everything read from a fifo (via tail -f ) through a pipe to the program

Usage: fi2sti <fifo> <prog> <prog_arguments>

where

  • fifo is a with "mkfifo" or "mknod p" created FIFO file
  • prog is the program which shall have stdin replaced in favor of the <fifo> file
    • every further arguments are getting parsed, without modification, to the program <prog>

Why not just pipe it like: /bin/bash prog args < fifo

As soon as one write to the fifo file (like echo help > fifo) is complete, the fifo signals an eof and the underlying program prog might stop or lose it's stdin.

So how is it solved here?

The way to solve this generally is tail -f fifo | prog args.
tail -f fifo will normally not stop reading out the growing lines of the specified file, except it receives a signal like SIGINT, SIGTERM, etc. The pipe between tail and prog (under normal circumstances) is therefore not destroyed so even after you are done writing to the fifo you can open it again, write something to it and the text is being transmitted to stdin of the program prog.

And that's almost what this program does, except it varies a little in order to make life a little easier when dealing with start-stop-daemon.
The start-stop-daemon only closes the process with a specified pid, so having that in mind this program does the following:

How it works:

First it opens the program "tail" by creating a pipe, forking and invoking the shell (via popen()).
"tail" is also being told to terminate if the process with the parent pid dies (via --pid=<parent_pid>).
A file descriptor is being created from the output stream of "tail".
The stdin file descriptor of the parent process is being copied to the file descriptor of the output of "tail" (via dup2()).
Lastly the program <prog> is being launched with execvp.

About

redirect stdin of a program to a fifo - designed for start-stop-daemon

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published