📄 test9.c
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <errno.h>#include <string.h>#include <sys/stat.h>#include <syslog.h>int init_daemon(const char *prog, int facility);int main(int argc, char **argv){ init_daemon(argv[0], LOG_USER); sleep(10); // void syslog(int priority, const char *message, ... /* arguments */); syslog(LOG_ERR | LOG_USER, "Just a test."); closelog(); return 0;}int init_daemon(const char *prog, int facility){ // step 1, fork(), parent process terminate pid_t pid; if ((pid = fork()) < 0) { fprintf(stderr, "ERROR: call fork() failed: %s, errno = %d\n", strerror(errno), errno); // FIXME: handle this error. exit(1); } if (pid > 0) { // XXX: parent process exit(0); } // step 2, set session id setsid(); // step 2.5, set signal handler // FIXME: process other signals //signal(SIGHUP, SIG_IGN); // step 3, change work dir chdir("/tmp"); // step 4, set file create mask umask(0); // step 5, close all opened files. int i; // FIXME: calculate the maximum file descriptior number int fdlimit = sysconf(_SC_OPEN_MAX); for (i = 0; i < fdlimit; i++) { close(i); } // step 6, open syslog // void openlog(const char *ident, int logopt, int facility); //openlog(prog,,); openlog(prog, LOG_PID, facility); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -