LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-11-2007, 05:44 PM   #1
inakizi
LQ Newbie
 
Registered: Mar 2007
Distribution: Debian
Posts: 4

Rep: Reputation: 0
Handling stdin and stdout in a daemon


I'm trying to do an application that converts applications into daemons (more or less). I basically have several programs (around 10) that are basically console programs with a menu. For example if you press 's' the program will give you statistics. Now I have to run all the programs simultaneously. I would like to have all the programs running as daemons but if I require to "connect" to the programs then I can (maybe by process Id or some other identifier) pipe the current cosole to the specific daemon, as if I'm running that program in the current console. Then I close that connection and I connect to another daemon. I know I can program that into each one of the programs, but I would rather not. I would like not to modifiy the programs at all but have a main program that does something like a popen to each process and then I can redirect those pipes to a console. Somebody knows how can I do that?
 
Old 12-11-2007, 05:55 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Run them is screen sessions. Then you can connect/disconnect/reconnect to them arbitrarily. If you make screen multi-user, you can allow anyone to connect, or a specified selection of users to access the 'daemons'.

man screen

--- rod.
 
Old 12-11-2007, 07:33 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Don't forget that daemons should change their working directory to / so they don't prevent the administrator from un-mounting volumes and so on.
 
Old 12-12-2007, 08:49 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
oh, quite simple then?

screen no good?

are they persistent, like overnight, or just while you are logged in and playing with them?

ok, each daemon process starts off and makes a FIFO or named pipe or Unix domain socket,
based on pid, these live in a certain directory, your selector program then can scan the directory
and allow you to choose which process to pipe to.
easy
 
Old 12-12-2007, 11:13 PM   #5
inakizi
LQ Newbie
 
Registered: Mar 2007
Distribution: Debian
Posts: 4

Original Poster
Rep: Reputation: 0
I'm doing something like that right now. I have this program that creates one thread for each daemon that I have to run. Then I create two pipes and then close(0 and 1) dup2 fork execve. After that each thread creates two named fifos and then they enter in a loop that reads from one of the named fifos and copies into the pipe corresponding to stdin and viceversa. As long as I catch the SIGPIPE signal and I check for the errors it works, but I think is too complicated, I was looking something more obvious like going into /proc/fd and try to replace the symlinks to 0,1,2 (already tried but couldn't make it work). Then I started looking at the source of screen and dtach (similar to screen but simpler) and they basically do something like what I do but using a lot of PTY (that I'm not very familiar with).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture both stdin and stdout of a child process? jineshkj Linux - Software 2 04-21-2006 06:34 AM
Details of stdin and stdout pixellany Linux - Newbie 1 01-11-2006 01:21 PM
stdout stdin Furlinastis Linux - Newbie 3 08-11-2005 11:00 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM
stdin -> port -> stdout acid_kewpie Linux - Networking 5 04-12-2002 06:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration