Advanced Programming in UNIX Environment Episode 68
发布日期:2021-10-07 23:47:42 浏览次数:1 分类:技术文章

本文共 2712 字,大约阅读时间需要 9 分钟。

In addition to syslog, many platforms provide a variant that handles variable argument lists.

#include 
#include
void vsyslog(int priority, const char *format, va_list arg);

All four platforms described in this book provide vsyslog, but this function is not included in the Single UNIX Specification. Note that to make its declaration visible to your application, you might need to define an additional symbol, such as __BSD_VISIBLE on FreeBSD or __USE_BSD on Linux.

Single-Instance Daemons

Some daemons are implemented so that only a single copy of the daemon should be running at a time for proper operation. Such a daemon might need exclusive access to a device, for example. In the case of the cron daemon, if multiple instances were running, each copy might try to start a single scheduled operation, resulting in duplicate operations and probably an error.

#include 
#include
#include
#include
#include
#include
#include
#include
#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)); return 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)); return 1; } ftruncate(fd,0); sprintf(buf, "%ld",(long)getpid()); write(fd,buf, strlen(buf)+1); return 0;}

Ensure that only one copy of a daemon is running

Daemon Conventions

Several common conventions are followed by daemons in the UNIX System.

  • If the daemon uses a lock file, the file is usually stored in /var/run.
  • If the daemon supports configuration options, they are usually stored in /etc. The configuration file is named name.conf, where name is the name of the daemon or the name of the service.
  • Daemons can be started from the command line, but they are usually started from one of the system initialization scripts (/etc/rc* or /etc/init.d/*). If the daemon should be restarted automatically when it exits, we can arrange for init to restart it if we include a respawn entry for it in /etc/inittab (assuming the system uses a System V style init command).
  • If a daemon has a configuration file, the daemon reads the file when it starts, but usually won’t look at it again. If an administrator changes the configuration, the daemon would need to be stopped and restarted to account for the configuration changes. To avoid this, some daemons will catch SIGHUP and reread their configuration files when they receive the signal.

转载地址:https://blog.csdn.net/myfather103/article/details/80003595 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Advanced Programming in UNIX Environment Episode 69
下一篇:Advanced Programming in UNIX Environment Episode 67

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月26日 09时38分20秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章