Writing a Daemon Process Debug Log
So Im starting up in some OS/Process programming and such, Im a newb at it but I have it mostly figured out, (my college OS book was useless so I had to browse the world wide web....yes I just called it that)
Anyways Here is my skeleton of a daemon process... It's mostly not mine, basically bits and pieces picked up from looking around in books example code (is it right?)
Anyways I had a few questions regarding session ID (what exactly is it? and how is it difference from Process ID)
and 2 is how would I go about logging debug files? in the section below? Because it's not in the daemon loop? would we just return stuff like "Daemon started: 11:59 am" etc...?
3, is why exactly do we change directories? and why would you want it to be root....Obviously Im not an x-pert linux programmer but still....
I guess I don't understand how this makes it a daemon? I understand forks and parents/children.....but what makes this a daemon?
#include <unistd.h>
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
int main(void) {
//Process and Session ID
pid_t pid, sid;
//Fork
pid = fork();
if (pid < 0) {
exit(EXIT_FA开发者_JAVA百科ILURE);
}
//Huzzah
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* APPARENTLY I PLACE LOGFILES HERE? HOW? */
//Create new sid (i don't understand this
sid = setsid();
if (sid < 0) {
exit(EXIT_FAILURE);
}
//Change Directory, Why?
if ((chdir("/")) < 0) {
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors <---Why? just for protection */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/* Daemon-specific initialization goes here */
//Daemon Loop
while (1) {
//Do Task
}
exit(EXIT_SUCCESS);
}
edit: Forgot to mention this is for Linux
Last question first: there is no strict definition of what qualifies a process as a daemon other than it runs in the background and without control by a user. But there are a bunch of characteristics that daemons usually have, some of which you have picked up on. This is a plain English starting point.
Process groups and sessions are a topic unto themselves. This is a decent enough jumping off point.
I don't know exactly what you are getting at with the log file question. Since daemons disassociate from a terminal stdin/out/err are redirected or closed. That leaves the problem of where to send output, in this case error messages. Syslog (or similar) facilities are usually employed but there is nothing stopping you from using any old file of your choosing. You would probably want to open them outside your event loop but there is nothing otherwise magical going on there - you just write to them.
The working directory is changed because you never know what the initial directory will be upon program initiation. It could be a network drive (or whatever) that can get dismounted while your (long running) daemon is still going. You can change the directory to whatever you want but root is commonly used because if the root directory disappears well...
If you find yourself getting serious about this stuff then get a good Stevens book, probably starting with APUE. They are pricey but one of the best investments of a lifetime.
精彩评论