forever c 代码实现demo
tdaemon.c源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/errno.h>
#include <signal.h>
#include "debug.h"
#define int4 int
static void ncSignal_fatal(int4 sig)
{
printf("catch signal (%d)\n",sig);
//exit(0);
}
static void ncSignal_hup(int4 sig)
{
return;
}
int4 server_start(char * shellname, char * saveshellname)
{
debug(LOG_DEBUG,"shell %s start",shellname);
int ret = system(shellname);
debug(LOG_DEBUG,"shell %s exits, exits code %d",shellname,ret);
if (saveshellname != NULL && strlen(saveshellname) > 0) {
debug(LOG_DEBUG,"run saveshell %s, saving subprocess failed env",saveshellname);
system(saveshellname);
}
return 0;
}
void dump_help() {
printf("tdaemon usage:\n \
-d open daemon model\n \
-dt daemon detect time\n \
-s daemon start shellname\n \
-sa daemon save failed env shellname\n \
-lg daemon log filename\n \
-h daemon help\n");
}
int4 main(int argc, char **argv)
{
int daemon = 0;
int4 sleeptime = 5;
char shellname[256] = {0};
char saveshellname[256] = {0};
char logfile[256] = {0};
int i=0;
for (i=0;i<argc;i++) {
if (strcmp(argv[i],"-d") == 0) {
daemon = 1;
} else if (strcmp(argv[i],"-dt") == 0) {
i++;
sleeptime = atoi(argv[i]);
} else if (strcmp(argv[i],"-s") == 0) {
i++;
strncpy(shellname,argv[i],255);
} else if (strcmp(argv[i],"-h") == 0) {
dump_help();
exit(0);
} else if (strcmp(argv[i],"-sa") == 0) {
i++;
strncpy(saveshellname,argv[i],255);
} else if (strcmp(argv[i],"-lg") == 0) {
i++;
strncpy(logfile,argv[i],255);
}
}
if (strlen(shellname) == 0) {
printf("shellname is null\n");
dump_help();
exit(0);
}
if (strlen(logfile) > 0) {
nc_set_log_file(logfile);
}
nc_set_debug(1);
debug(LOG_DEBUG,"daemon:%d,detect time:%d,shellname:%s",daemon,sleeptime,shellname);
int4 iPid;
iPid = fork();
if (iPid > 0)
{
exit(0);
}
else if (iPid == 0)
{
if (daemon == 1) {
int4 iPid1,iStatus;
signal(SIGHUP,ncSignal_hup);
signal(SIGINT,ncSignal_fatal);
signal(SIGQUIT,ncSignal_fatal);
signal(SIGILL,ncSignal_fatal);
signal(SIGTRAP,ncSignal_fatal);
signal(SIGFPE,ncSignal_fatal);
signal(SIGTERM,ncSignal_fatal);
signal(SIGPIPE,ncSignal_fatal);
while(1)
{
iPid = fork();
if (iPid == 0)
{
server_start(shellname,saveshellname);
exit(0);
}
else
{
while(1)
{
iPid1 = waitpid(-1,&iStatus,WNOHANG);
if (iPid1 > 0)
{
iStatus = WEXITSTATUS(iStatus);
printf("[waitpid]pid %d Exit Status is %d\n",iPid1,iStatus);
debug(LOG_DEBUG,"[waitpid]pid %d Exit Status is %d\n",iPid1,iStatus);
kill(iPid1,9);
break;
}
sleep(sleeptime);
}
}
}
} else {
server_start(shellname,saveshellname);
exit(0);
}
}
}
debug.h源代码
#ifndef __DEBUG_H__
#define __DEBUG_H__
#include <stdio.h>
#include <sys/syslog.h>
#define debug(level, format...) _debug(__FILE__, __LINE__, level, format)
void _debug(char *filename, int line, int level, char *format, ...);
void nc_set_log_file(char * filename);
void nc_set_debug(int flag);
#endif
debug.c源代码
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
int debug_flag = 0;
char log_file[256] = "tdaemon.log";
void _debug(char *filename, int line, int level, char *format, ...)
{
char buf[28];
va_list vlist;
time_t ts;
FILE * fd;
if (debug_flag)
{
time(&ts);
if ((fd = fopen(log_file,"a") ) != NULL)
{
fprintf(fd, "[%d][%.24s][%u](%s:%d) ", level, ctime_r(&ts, buf), getpid(),
filename, line);
va_start(vlist, format);
vfprintf(fd, format, vlist);
va_end(vlist);
fputc('\n', fd);
fflush(fd);
fclose(fd);
}
}
}
void nc_set_debug(int flag)
{
debug_flag = flag;
}
void nc_set_log_file(char * file)
{
strncpy(log_file, file, 255);
}
Makefile源代码
all:
gcc -o tdaemon ./tdaemon.c ./debug.c -I./
clean:
rm -f tdaemon
运行脚本代码
#!/bin/sh
./tdaemon -s ./start.sh -sa ./save.sh -lg tdaemon.log -d
start.sh为启动时执行的脚本,save.sh为程序退出时的执行脚本,-d为打开守护模式