Linux内核监控工具。 Linux内核监控工具。 Linux内核监控工具。

源代码在线查看: deamon.c

软件大小: 168 K
上传用户: red2years
关键词: Linux 内核 监控
下载地址: 免注册下载 普通下载 VIP

相关代码

				/***************************************************************************				                          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 				#endif												#include				#include   //for fork,getrlimit				#include  //for umask, open				#include  //for umask, open				#include  //for umask, open				#include  //for getrlimit				#include  //for getrlimit				#include  // for exit				#include "linux_mond.h"								extern char PIDDIR[]; //defined in linux_mon.c								int 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;				}											

相关资源