Você está na página 1de 13

Daemon Processes

Daemons are processes that live for long time


Mostly they are started when the system is
bootstrapped and terminate when the system
is shut down
Inetd, sshd, init, syslogd, etc.

Characteristics

Daemons run in background


They have super user privilege
Dont have controlling terminal
They are session and group leaders

Daemon Process Coding Rules


Set file mode creation mask to 0 (umask)
Allowing daemon to set specific file permissions

Create child process (fork) and let parent process exit


pid != ppid in child process, child not group leader
Prerequisite for start a new session

Create a new session (setsid)


Becomes session leader, has no controlling terminal
Step 2 is recommended again (fork and exit parent process)

Change working directory to root directory


Allows mounted file system to be unmounted
This step depends on specifics of daemon process

Unneeded file descriptors should be closed


Redirect descriptors 0, 1, 2 to /dev/null

How to Daemonize Process


umask(0); // clear file creation mask
if ((pid = fork()) < 0) return -1;
if (pid) exit(0); // exit the parent process

// now child is not group leader


// create a new session and become session leader
// new session does not have controlling terminal
if (setsid() < 0) return -1;
signal(SIGHUP, SIG_IGN); // ignoring SIGHUP
If ((pid = fork()) < 0) return -1;
If (pid) exit(0); // exit first child process to ensure we will not
// accidentally obtain controlling terminal
chdir(/); // change working directory to root directory
close(all file descriptors);
// redirect stdin, stdout, stderr
open(/dev/null, O_RDONLY);
open(/dev/null, RDWR);
open(/dev/null, RDWR);
openlog(); //preparing log file
return(0);
4

Example
#include <unistd,h>
#include <sys/types.h>
#include <fcntl.h>
int daemon_initialise( )
{
pid_t pid;
if (( pid = fork() ) < 0)
return 1;
else if ( pid != 0)
exit(0); /* parent exits */
/* child continues */
setsid( );
chdir(/);
umask(0);
return 0;
}

Error Logging
How to output messages when something
happens?
Daemon processes do not have controlling
terminal
Dont want each daemon to have its own log file
A central daemon error-logging facility

syslogd
A daemon process waiting for messages from
other processes

Syslogd Daemon
Different ways to communicate with syslogd
Unix domain socket
UDP socket (on port 514)
A file opened for accepting messages from kernel
/etc/syslog.conf

user process

syslogd

syslog

/dev/log
Unix domain
datagram socket

kernel

UDP port 514


Internet domain
datagram socket

/dev/klog
log

Kernel
routines

Syslog Function

#include <syslog.h>
void openlog (const char *ident, int option, int facility);
void syslog (int priority, const char *msg, );
void closelog (void);

Ident
Some identifier added to each log message
Option
Various control options
Facility specifies type of sending processes
LOG_CRON, LOG_FTP, LOG_KERN, LOG_MAIL, etc.
Priority
Combination of level and facility
8 levels are defined
LOG_WARNING, LOG_NOTICE (default), LOG_INFO, etc
openlog(lpd, LOG_PID, LOG_LPR);
syslog(LOG_ERR, open error for %s: %m, filename);
8

Single Instance
Some daemons are implemented so that only a single
copy of the daemon should be running at a time for
proper operation
The file and record-locking mechanism helps for this
which also provides mutual-exclusion mechanism
If the daemon obtains a write-lock on an entire file, the
lock will be removed automatically if the daemon exits
This simplifies recovery, removing the need for us to
clean up from the previous instance of the daemon
Example.c

PROGRAM:Ensure that only one copy of a daemon is running

#define LOCKFILE "/var/run/daemon.pid"


#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
extern int lockfile(int);
int already_running(void)
{
int fd;
char buf[16];
fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
if (fd < 0)
{
syslog(LOG_ERR, "can't open %s: %s", LOCKFILE,
strerror(errno));
exit(1);
}

if (lockfile(fd) < 0)
{
if (errno == EACCES || errno == EAGAIN)
{
close(fd);
return(1);
}
syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE,
strerror(errno));
exit(1);
}
ftruncate(fd, 0);
sprintf(buf, "%ld", (long) getpid());
write(fd, buf, strlen(buf)+1);
return(0);
}

Daemon conventions
Lockfile, the file is usually stored in
/var/run/name.pid
Configuration options in /etc/name.conf
Daemons started at command line /etc/init.d
Daemons restart -> make respawn entry in
/etc/inittab
For safe use of Daemons use SIGHUP signal

Client-Server model
A server is a process that waits for a client to
contact it, requesting some type of service
Most of the time, the client server model
works as a one way communication between
client and server

Você também pode gostar