Run as a daemon or service: Difference between revisions

→‎{{header|C}}: Describe BSDisms. Keep dup2() in program.
(Add C, with the rather unnecessary stdout redirection.)
(→‎{{header|C}}: Describe BSDisms. Keep dup2() in program.)
Line 9:
 
=={{header|C}}==
==={{libheader|BSD libc}}===
Most daemons have no stdout, but this draft task strangely wants to use stdout, so this program has a weird dup2() call. (Perhaps the task should remove its spurious stdout requirement.)
[[BSD]] provides a convenient [http://netbsd.gw.com/cgi-bin/man-cgi?daemon+3+NetBSD-current daemon(3)] function. [[GNU]] libc also provides [http://www.kernel.org/doc/man-pages/online/pages/man3/daemon.3.html daemon(3)], but [[POSIX]] omits it, so it is not portable. Other BSDisms in this program are __progname and <err.h>.
 
The task also wants to redirect stdout. This program does so with [http://netbsd.gw.com/cgi-bin/man-cgi?dup2+2+NetBSD-current dup2(2)]. If we omit the requirement to redirect stdout; we can simplify the program by removing dup2(), by opening our file with <code>file = fopen(argv[1], "a")</code>, and by writing to ''file'' instead of ''stdout''.
 
{{libheader|BSD libc}}
<lang c>#include <err.h>
#include <errno.h>
Anonymous user