新闻  |   论坛  |   博客  |   在线研讨会
linux 守护程序
tongxin | 2009-04-12 23:41:14    阅读:1231   发布文章

#include <stdio.h>
%A #include <stdlib.h>
%A #include <unistd.h>
%A #include <dirent.h>
%A #include <errno.h>
%A #include <fcntl.h>
%A #include <ctype.h>
%A #include <string.h>
%A #include <sys/ioctl.h>
%A #include <sys/wait.h>
%A
%A #define PROC_NAME     "mymail"
%A #define EXEC_NAME     "/root/dev_mobile/source/MyMail/src/mymail"
%A #define   LOCKFILE "/root/mytest.lock"
%A #define  DEBUG
%A int daemon_init()
%A {
%A     struct sigaction act;
%A     int i, maxfd;
%A     int lock_fd;
%A     int ret;
%A     char buf[100];
%A     lock_fd=open(LOCKFILE,O_RDWR|O_CREAT,0640);
%A     if(lock_fd<0){
%A         printf("lockfile failed :\n");
%A             exit(-1);
%A     }
%A     
%A     ret=flock(lock_fd,LOCK_EX|LOCK_NB);//因为下面关闭了,所以起不到作用
%A     if(ret<0){
%A         printf("can‘t obtaion the file lock:\n");
%A         exit(0);
%A     }
%A     if (fork() != 0)
%A         exit(0);
%A     
%A     if (setsid() < 0)
%A         return -1;
%A     act.sa_handler = SIG_IGN;
%A     sigemptyset(&act.sa_mask);
%A     act.sa_flags = 0;
%A     sigaction(SIGHUP, &act, 0);
%A     if (fork() != 0)
%A         exit(0);
%A     chdir("/");
%A     umask(0);
%A     setpgrp();
%A     maxfd = sysconf(_SC_OPEN_MAX);
%A     for (i = 0; i < maxfd; i++)
%A         //close(i);
%A         ;
%A     sprintf(buf,"%6d\n",getpid());
%A     write(lock_fd,buf,strlen(buf));    
%A     open("/dev/null", O_RDWR);
%A     dup(0);
%A     dup(1);
%A     dup(2);
%A     return 0;
%A }
%A static int file2str(char *filename, char *ret, int cap)
%A {
%A     int fd, num_read;
%A
%A     if ((fd = open(filename, O_RDONLY, 0)) == -1)
%A         return -1;
%A     if ((num_read = read(fd, ret, cap - 1)) <= 0)
%A         return -1;
%A     ret[num_read] = 0;
%A     close(fd);
%A     return num_read;
%A }
%A
%A static int parse_proc_status(char*p_name,char *S)
%A {
%A     char *tmp;
%A     unsigned char p_id[10];
%A     memset(p_id, 0x00, sizeof(p_id));
%A     sscanf(S, "Name:\t%15c", p_id);
%A     tmp = strchr(p_id, ‘\n‘);
%A     if (tmp)
%A         *tmp = ‘\0‘;
%A     //printf("%s\n",p_id);
%A     if(strcmp(p_name,p_id)==0)
%A         return 0;
%A     else
%A          return -1;
%A     return -1;
%A     
%A }
%A
%A static int proc_exist(char *p_name)
%A {
%A   DIR *dir;
%A   FILE *file;
%A   struct dirent *entry;
%A   char path[32], sbuf[512];
%A   int id;
%A     dir = opendir("/proc");
%A     if (!dir){
%A         perror("Can‘t open /proc");
%A         exit(-1);
%A     }
%A         while ((entry = readdir(dir)) != NULL) {
%A         if (!isdigit(*entry->d_name))
%A             continue;
%A         sprintf(path, "/proc/%s/status", entry->d_name);
%A         if ((file2str(path, sbuf, sizeof sbuf)) != -1) {
%A             if(parse_proc_status(p_name,sbuf)==0){
%A                 id=atol(entry->d_name);
%A                 printf("id=%d\n",id);
%A                 closedir(dir);
%A                 return 0;
%A             }
%A                 
%A         }
%A     }
%A closedir(dir);
%A return -1;
%A }
%A
%A static int proc_fork(char*p_path)
%A {
%A char * argv[ ]={PROC_NAME,(char*)0 };
%A pid_t pid;
%A pid=fork();
%A if(pid==0){
%A execv(p_path,argv);
%A }
%A if(pid>0)
%A     printf("fork  succeed:\n");
%A }
%A
%A
%A void handler()
%A {
%A   char uidName[30];
%A   char exec_path[50];
%A
%A     memset(uidName,0x00,sizeof(uidName));
%A     memset(exec_path,0x00,sizeof(exec_path));
%A     strcpy(uidName,PROC_NAME);
%A     strcpy(exec_path,EXEC_NAME);
%A     printf("one\n");
%A     if (proc_exist(uidName)==0)
%A         printf("进程存在\n");
%A     else
%A         proc_fork(exec_path);    
%A     alarm(10);
%A }
%A
%A int main(int argc, char **argv)
%A {
%A #ifdef DEBUG
%A    if (daemon_init() < 0) {
%A         printf("daemon failed\n");
%A         return 1;
%A     }
%A #endif
%A    signal(SIGALRM,handler);
%A    alarm(15);
%A    while (1){
%A    while ( waitpid(-1,NULL,WNOHANG) > 0)
%A         printf("test:\n");
%A    }
%A }
%A
%A 网上的unix比较多,前几天做了一个linux下的,监控某一程序,如果不存在,就启动。
%A
%A
%A
%A
%A%A
%A

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
最近文章
寂寞如雪
2009-05-19 19:01:18
夜色花
2009-05-19 18:56:22
没有爱可以重来
2009-05-19 18:54:59
推荐文章
最近访客