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

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

#include "apue.h"#include 
#include
#include
void deamonize(const char *cmd){ int i,fd0,fd1,fd2; pid_t pid; struct rlimit rl; struct sigaction sa; umask(0); if(getrlimit(RLIMIT_NOFILE,&rl)<0) err_quit("%s: can't get file limit", cmd); if((pid=fork())<0) err_quit("%s: cna't fork", cmd); else if(pid!=0) return 0; setsid(); sa.sa_handler=SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags=0; if(sigaction(SIGHUP, &sa,NULL)<0) err_quit("%s: can't ignore SIGHUP", cmd); if((pid=fork())<0) err_quit("%s: can't fork",cmd); else if(pid!=0) return 0; if(chdir("/")<0) err_quit("%s: can't change directory to /",cmd); if(rl.rlim_max==RLIM_INFINITY) rl.rlim_max=1025; for(i=0;i

Initialize a daemon process

Error Logging

It can’t simply write to

standard error, since it shouldn’t have a controlling terminal. We don’t want all the daemons writing to the console device, because on many workstations the console device runs a windowing system. We also don’t want each daemon writing its own error messages into a separate file. It would be a headache for anyone administering the system to keep up with which daemon writes to which log file and to check these files on a regular basis. A central daemon error-logging facility is required.

The BSD syslog facility was developed at Berkeley and used widely in 4.2BSD. Most systems derived from BSD support syslog. Until SVR4, System V never had a central daemon logging facility. The syslog function is included in the XSI option in the Single UNIX Specification.

The BSD syslog facility has been widely used since 4.2BSD. Most daemons use this facility.

There are three ways to generate log messages:

1.Kernel routines can call the log function. These messages can be read by any user process that opens and reads the /dev/klog device. We won’t describe this function any further, since we’re not interested in writing kernel routines.

2.Most user processes (daemons) call the syslog(3) function to generate log messages. We describe its calling sequence later. This causes the message to be sent to the UNIX domain datagram socket /dev/log.
3.A user process on this host, or on some other host that is connected to this host by a TCP/IP network, can send log messages to UDP port 514. Note that the syslog function never generates these UDP datagrams: they require explicit network programming by the process generating the log message.

Refer to Stevens, Fenner, and Rudoff [2004] for details on UNIX domain sockets and UDP sockets.

Our interface to this facility is through the syslog function.

#include 
void openlog(const char *ident, int option, int facility);void syslog(int priority, const char *format, ...);void closelog(void);int setlogmask(int maskpri);

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

上一篇:Advanced Programming in UNIX Environment Episode 68
下一篇:Advanced Programming in UNIX Environment Episode 66

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年02月29日 09时45分08秒

关于作者

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

推荐文章