📄 deamon.c
字号:
/*************************************************************************** deamon.c - description ------------------- begin : Thu Sep 27 08:08:03 EET 2001 copyright : (C) 2001 by Petri Turunen email : petri.turunen@pete.fi.eu.org ***************************************************************************//*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#include<stdio.h>#include <unistd.h> //for fork,getrlimit#include <sys/types.h> //for umask, open#include <sys/stat.h> //for umask, open#include <fcntl.h> //for umask, open#include <sys/time.h> //for getrlimit#include <sys/resource.h> //for getrlimit#include <stdlib.h> // for exit#include "linux_mond.h"extern char PIDDIR[]; //defined in linux_mon.cint make_deamon(void) //Make me daemon{ struct rlimit resourceLimit = { 0 }; int status = -1; int fileDesc = -1; int i = 0; FILE *pid_file; char pidfile[30]="\0"; status = fork(); switch (status) { case -1: slog(2,"deamon.c: fork() failed in make_deamon"); exit(-1); case 0: /* child process */ break; default: /* parent process */ exit(0); } /* * child process */ resourceLimit.rlim_max = 0; status = getrlimit(RLIMIT_NOFILE, &resourceLimit); if (-1 == status) /* shouldn't happen */ { slog(2,"deamon.c: getrlimit() failed in make_deamon"); exit(-1); } if (0 == resourceLimit.rlim_max) { slog(2,"deamon.c: Error max number of open file descriptors is 0!!"); exit(-1); } for (i = 0; i < resourceLimit.rlim_max; i++) { (void) close(i); } status = setsid(); if (-1 == status) { slog(2,"deamon.c: setsid() failed in make_deamon"); exit(-1); } status = fork(); switch (status) { case -1: slog(2,"deamon.c: fork() failed in make_deamon"); exit(-1); case 0: /* (second) child process */ break; default: /* parent process */ exit(0); } /* * now we are in a new session and process * group than process that started the * daemon. We also have no controlling * terminal */ chdir("/tmp/"); if(status != 0) { slog(2,"Failed to change directory to /tmp. %s",strerror(errno)); printf("Failed to change directory to /tmp. %s\n",strerror(errno)); exit(-1); } umask(0); fileDesc = open("/dev/null", O_RDWR);/* stdin */ (void) dup(fileDesc); /* stdout */ (void) dup(fileDesc); /* stderr */ strcat(pidfile,PIDDIR); strcat(pidfile,"/linux_mond.pid"); pid_file = fopen(pidfile,"w"); if(pid_file==NULL) slog(2,"deamon.c: Error while opening %s. %s",pidfile, strerror(errno)); else { fprintf(pid_file, "%d",getpid()); fclose(pid_file); } /* * the rest of the daemon code executes in * this environment */ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -