Escolar Documentos
Profissional Documentos
Cultura Documentos
Characteristics
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
/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
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