digitalmars.D.learn - Daemon app in D
- Geert (2/2) Jun 07 2005 Is it possible to create a (linux) daemon in D? And if; how?
- Dejan Lekic (6/6) Jun 07 2005 Read samples in DMD distribution - there is one example there I think.
- Geert (3/5) Jun 07 2005 No, it is a windows server or something..
- James Dunne (6/11) Jun 07 2005 I've done this before, and it needs to be done with linux system calls. ...
- Geert (2/19) Jun 07 2005
- Dejan Lekic (7/7) Jun 07 2005 So you cannot write an simple D application that reads stdin, writes to
- Geert (3/6) Jun 07 2005 It doesn't really detach the app from the shell.. Try for instance
- SeeSchloss (10/13) Jun 07 2005 An
- SeeSchloss (2/3) Jun 07 2005 or rather "extern (C) int fork ();", sorry
- Geert (13/20) Jun 07 2005 Thanks, but the program will continue to be connected to the shell try
- SeeSchloss (4/18) Jun 07 2005 Well yes it's still attached to the shell of its parent process (until t...
- Dejan Lekic (7/7) Jun 07 2005 Geert try to write to some log-file and most likely it will continue to ...
- James Dunne (36/43) Jun 07 2005 The correct way to detach from the shell (and I've written a server app ...
- James Dunne (109/154) Jun 07 2005 And, as promised (later than I had hoped for - I apologize), your linux ...
- Geert (2/170) Jun 08 2005
Is it possible to create a (linux) daemon in D? And if; how? Thnx
Jun 07 2005
Read samples in DMD distribution - there is one example there I think. -- ........... Dejan Lekic http://dejan.lekic.org
Jun 07 2005
No, it is a windows server or something.. I need a real daemon that disconnects from the shell. Dejan Lekic wrote:Read samples in DMD distribution - there is one example there I think.
Jun 07 2005
In article <d840t0$2881$1 digitaldaemon.com>, Geert says...No, it is a windows server or something.. I need a real daemon that disconnects from the shell. Dejan Lekic wrote:I've done this before, and it needs to be done with linux system calls. There's no real nice D way to do it. I can't dig up an example of mine for you right now, but I'll get one for you later. Regards, James DunneRead samples in DMD distribution - there is one example there I think.
Jun 07 2005
Thanks, that would be great... James Dunne wrote:In article <d840t0$2881$1 digitaldaemon.com>, Geert says...No, it is a windows server or something.. I need a real daemon that disconnects from the shell. Dejan Lekic wrote:I've done this before, and it needs to be done with linux system calls. There's no real nice D way to do it. I can't dig up an example of mine for you right now, but I'll get one for you later. Regards, James DunneRead samples in DMD distribution - there is one example there I think.
Jun 07 2005
So you cannot write an simple D application that reads stdin, writes to stdout, uses inetd and you run it via "myprog [params] &" ? -- ........... Dejan Lekic http://dejan.lekic.org
Jun 07 2005
It doesn't really detach the app from the shell.. Try for instance typing Ctrl-Z and then fg, it will bring back the app to the foreground. Dejan Lekic wrote:So you cannot write an simple D application that reads stdin, writes to stdout, uses inetd and you run it via "myprog [params] &" ?
Jun 07 2005
On Tue, 07 Jun 2005 12:56:22 +0200, Geert wrote:Is it possible to create a (linux) daemon in D? And if; how? ThnxAn extern (C) fork (); somewhere, and then if (fork ()) exit (0); at the place you want your daemon to daemonize should be enough (since the point is that processes of which the parents are dead are adopted by init, and therefore become deamons). That's what my server uses, and that's the standard way to do it afaict. SeeSchloss.
Jun 07 2005
On Tue, 07 Jun 2005 18:42:36 +0200, SeeSchloss wrote:extern (C) fork ();or rather "extern (C) int fork ();", sorry
Jun 07 2005
Thanks, but the program will continue to be connected to the shell try this for instance: extern (C) int fork (); void main() { // fork returns -1 on failure // 0 to childprocess, and the pid // number of the child process to the parent if(fork()) exit(0); while(1) printf("You will see this output.\n"); } SeeSchloss wrote:On Tue, 07 Jun 2005 18:42:36 +0200, SeeSchloss wrote:extern (C) fork ();or rather "extern (C) int fork ();", sorry
Jun 07 2005
On Tue, 07 Jun 2005 19:07:47 +0200, Geert wrote:Thanks, but the program will continue to be connected to the shell try this for instance: extern (C) int fork (); void main() { // fork returns -1 on failure // 0 to childprocess, and the pid // number of the child process to the parent if(fork()) exit(0); while(1) printf("You will see this output.\n"); }Well yes it's still attached to the shell of its parent process (until the shell is closed, of course), but a daemon doesn't print on stdout anyway, so that shouldn't be a problem...
Jun 07 2005
Geert try to write to some log-file and most likely it will continue to fill it with bytes after you close the shell. :) -- ........... Dejan Lekic http://dejan.lekic.org
Jun 07 2005
The correct way to detach from the shell (and I've written a server app that does this, just don't have the code with me ATM (at work)) is to create a different session ID and then fork and kill the parent. You might want to install some signal handlers in your server if you go this way. In your signal handler for SIGINT and SIGTERM you'll want to copy/paste the code from internal/dmain2.d in phobos that terminates D: : _moduleDtor(); : gc_term(); : } : catch (Object o) : { : printf("Error: "); : o.print(); : exit(EXIT_FAILURE); : } : : version (linux) : { : free(am); : _STD_critical_term(); : _STD_monitor_staticdtor(); : } I recall that it was a true pain to get D to terminate the program correctly when you have multiple threads running and you're interrupted by a signal. Horrible stuff. I wish phobos had a nice "guaranteed cleanup" system, in case of emergency death of app. It is possible to do this easily in your own application, but you'd have to create a Windows-specific and a Linux-specific version of the code that does this. Phobos just feels like a great place for this functionality. If this becomes the case, I have some code for Linux I'd like to contribute! However, I don't know jack squat about the Windows side of things... P.S. - I promise you I'll get you the code for this when I get home (in 4 hours or so). In article <d84lom$2ql7$1 digitaldaemon.com>, Dejan Lekic says...Geert try to write to some log-file and most likely it will continue to fill it with bytes after you close the shell. :) -- ........... Dejan Lekic http://dejan.lekic.orgRegards, James Dunne
Jun 07 2005
And, as promised (later than I had hoped for - I apologize), your linux daemon in D: :import std.c.stdlib; :version (linux) import std.c.linux.linux; : :extern (C) { : /// These are for control of termination: : void _STD_monitor_staticdtor(); : void _STD_critical_term(); : void gc_term(); : void _moduleDtor(); : : version (linux) { : alias int pid_t; : : // daemon functions: : pid_t fork(); : int umask(int); : int setsid(); : int close(int fd); : : // Signal trapping in Linux: : typedef void (*sighandler_t)(int); : sighandler_t signal(int signum, sighandler_t handler); : : void sighandler(int sig) { : write_log("signal %d caught...", sig); : terminate(); : } : } :} ; :void terminate() { : // Custom termination code here: : : : // Phobos termination (since we can't really make main() return): : _moduleDtor(); : gc_term(); : version (linux) { : //free(am); : _STD_critical_term(); : _STD_monitor_staticdtor(); : } : : exit(0); :} : :int main(char[][] args) { : debug { : } else { : // Daemonize under Linux: : version (linux) { : // Our process ID and Session ID : pid_t pid, sid; : : // Fork off the parent process : pid = fork(); : if (pid < 0) { : exit(EXIT_FAILURE); : } : // If we got a good PID, then we can exit the parent :process. : if (pid > 0) { : exit(EXIT_SUCCESS); : } : : // Change the file mode mask : umask(0); : } : } : : // Open a log file here: : : : debug { } else { : version (linux) { : // Create a new SID for the child process : sid = setsid(); : if (sid < 0) terminate(); : : // Close out the standard file descriptors : close(0); : close(1); : close(2); : } : } : : version (linux) { : signal(SIGABRT, &sighandler); : signal(SIGTERM, &sighandler); : signal(SIGQUIT, &sighandler); : signal(SIGINT, &sighandler); : } : : try { : // Do your main loop here : } catch (ex as Exception) { : // Catch an exception : } finally { : // Terminate the server : terminate(); : } : : return 0; :} Hope this helps! =) In article <d84psn$2uga$1 digitaldaemon.com>, James Dunne says...The correct way to detach from the shell (and I've written a server app that does this, just don't have the code with me ATM (at work)) is to create a different session ID and then fork and kill the parent. You might want to install some signal handlers in your server if you go this way. In your signal handler for SIGINT and SIGTERM you'll want to copy/paste the code from internal/dmain2.d in phobos that terminates D: : _moduleDtor(); : gc_term(); : } : catch (Object o) : { : printf("Error: "); : o.print(); : exit(EXIT_FAILURE); : } : : version (linux) : { : free(am); : _STD_critical_term(); : _STD_monitor_staticdtor(); : } I recall that it was a true pain to get D to terminate the program correctly when you have multiple threads running and you're interrupted by a signal. Horrible stuff. I wish phobos had a nice "guaranteed cleanup" system, in case of emergency death of app. It is possible to do this easily in your own application, but you'd have to create a Windows-specific and a Linux-specific version of the code that does this. Phobos just feels like a great place for this functionality. If this becomes the case, I have some code for Linux I'd like to contribute! However, I don't know jack squat about the Windows side of things... P.S. - I promise you I'll get you the code for this when I get home (in 4 hours or so). In article <d84lom$2ql7$1 digitaldaemon.com>, Dejan Lekic says...Regards, James DunneGeert try to write to some log-file and most likely it will continue to fill it with bytes after you close the shell. :) -- ........... Dejan Lekic http://dejan.lekic.orgRegards, James Dunne
Jun 07 2005
Thanks it sure helps! James Dunne wrote:And, as promised (later than I had hoped for - I apologize), your linux daemon in D: :import std.c.stdlib; :version (linux) import std.c.linux.linux; : :extern (C) { : /// These are for control of termination: : void _STD_monitor_staticdtor(); : void _STD_critical_term(); : void gc_term(); : void _moduleDtor(); : : version (linux) { : alias int pid_t; : : // daemon functions: : pid_t fork(); : int umask(int); : int setsid(); : int close(int fd); : : // Signal trapping in Linux: : typedef void (*sighandler_t)(int); : sighandler_t signal(int signum, sighandler_t handler); : : void sighandler(int sig) { : write_log("signal %d caught...", sig); : terminate(); : } : } :} ; :void terminate() { : // Custom termination code here: : : : // Phobos termination (since we can't really make main() return): : _moduleDtor(); : gc_term(); : version (linux) { : //free(am); : _STD_critical_term(); : _STD_monitor_staticdtor(); : } : : exit(0); :} : :int main(char[][] args) { : debug { : } else { : // Daemonize under Linux: : version (linux) { : // Our process ID and Session ID : pid_t pid, sid; : : // Fork off the parent process : pid = fork(); : if (pid < 0) { : exit(EXIT_FAILURE); : } : // If we got a good PID, then we can exit the parent :process. : if (pid > 0) { : exit(EXIT_SUCCESS); : } : : // Change the file mode mask : umask(0); : } : } : : // Open a log file here: : : : debug { } else { : version (linux) { : // Create a new SID for the child process : sid = setsid(); : if (sid < 0) terminate(); : : // Close out the standard file descriptors : close(0); : close(1); : close(2); : } : } : : version (linux) { : signal(SIGABRT, &sighandler); : signal(SIGTERM, &sighandler); : signal(SIGQUIT, &sighandler); : signal(SIGINT, &sighandler); : } : : try { : // Do your main loop here : } catch (ex as Exception) { : // Catch an exception : } finally { : // Terminate the server : terminate(); : } : : return 0; :} Hope this helps! =) In article <d84psn$2uga$1 digitaldaemon.com>, James Dunne says...The correct way to detach from the shell (and I've written a server app that does this, just don't have the code with me ATM (at work)) is to create a different session ID and then fork and kill the parent. You might want to install some signal handlers in your server if you go this way. In your signal handler for SIGINT and SIGTERM you'll want to copy/paste the code from internal/dmain2.d in phobos that terminates D: : _moduleDtor(); : gc_term(); : } : catch (Object o) : { : printf("Error: "); : o.print(); : exit(EXIT_FAILURE); : } : : version (linux) : { : free(am); : _STD_critical_term(); : _STD_monitor_staticdtor(); : } I recall that it was a true pain to get D to terminate the program correctly when you have multiple threads running and you're interrupted by a signal. Horrible stuff. I wish phobos had a nice "guaranteed cleanup" system, in case of emergency death of app. It is possible to do this easily in your own application, but you'd have to create a Windows-specific and a Linux-specific version of the code that does this. Phobos just feels like a great place for this functionality. If this becomes the case, I have some code for Linux I'd like to contribute! However, I don't know jack squat about the Windows side of things... P.S. - I promise you I'll get you the code for this when I get home (in 4 hours or so). In article <d84lom$2ql7$1 digitaldaemon.com>, Dejan Lekic says...Regards, James DunneGeert try to write to some log-file and most likely it will continue to fill it with bytes after you close the shell. :) -- ........... Dejan Lekic http://dejan.lekic.orgRegards, James Dunne
Jun 08 2005