⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpudated.c

📁 基于网络编程的例子
💻 C
字号:
/* * lpudated.c - Simple timestamping daemon */#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <errno.h>#include <unistd.h>#include <time.h>#include <syslog.h>int main(void){    pid_t pid, sid;    time_t timebuf;    int fd, len;         pid = fork();    if(pid < 0) {    syslog(LOG_ERR, "%s\n", perror);    exit(EXIT_FAILURE);    }    if(pid > 0)     /* In the parent, let's bail */        exit(EXIT_SUCCESS);    /* In the child... */    /* Open the system log */    openlog("lpudated", LOG_PID, LOG_DAEMON);    /* First, start a new session */    if((sid = setsid()) < 0) {        syslog(LOG_ERR, "%s\n", "setsid");        exit(EXIT_FAILURE);    }    /* Next, make / the current directory */    if((chdir("/")) < 0) {        syslog(LOG_ERR, "%s\n", "chdir");        exit(EXIT_FAILURE);    }    /* Reset the file mode */    umask(0);    /* Close unneeded file descriptors */    close(STDIN_FILENO);    close(STDOUT_FILENO);    close(STDERR_FILENO);    /*  Finally, do our work */    len = strlen(ctime(&timebuf));    while(1) {        char *buf = malloc(sizeof(char) * (len + 1));            if(buf == NULL) {            syslog(LOG_ERR, "malloc");            exit(EXIT_FAILURE);        }        if((fd = open("/var/log/lpudated.log",                       O_CREAT | O_WRONLY | O_APPEND, 0600)) < 0) {            syslog(LOG_ERR, "open");            exit(EXIT_FAILURE);        }        time(&timebuf);        strncpy(buf, ctime(&timebuf), len + 1);        write(fd, buf, len + 1);        close(fd);        sleep(60);    }    /* Close the system log and scram */    closelog();    exit(EXIT_SUCCESS);}     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -