158 lines
3.4 KiB
C
158 lines
3.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <signal.h>
|
|
#include <string.h>
|
|
|
|
#define CREAT_PID_FILE_PATH "/tmp/mydaemon.pid"
|
|
#define LOG_FILE_NAME "log.txt"
|
|
#define SUCCESS() fprintf(stdout, "success\n")
|
|
void daemonize(const char *cmd)
|
|
{
|
|
pid_t pid, sid;
|
|
|
|
// Fork off the parent process
|
|
pid = fork();
|
|
// An error occurred
|
|
if (pid < 0)
|
|
{
|
|
|
|
fprintf(stderr, "can not creat process\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
// Success: Let the parent terminate
|
|
if (pid > 0)
|
|
{
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
// On success: The child process becomes session leader
|
|
if ((sid = setsid()) < 0)
|
|
{
|
|
fprintf(stderr, "can not new secion\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Catch, ignore and handle signals
|
|
signal(SIGCHLD, SIG_IGN);
|
|
signal(SIGHUP, SIG_IGN);
|
|
// Fork off for the second time
|
|
pid = fork();
|
|
|
|
// An error occurred
|
|
if (pid < 0)
|
|
{
|
|
fprintf(stderr, "can not creat process\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Success: Let the parent terminate
|
|
if (pid > 0)
|
|
{
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
// Set new file permissions
|
|
umask(0);
|
|
|
|
// Change the working directory to the root directory
|
|
// or another appropriated directory
|
|
if ((chdir("/")) < 0)
|
|
{
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Open the /dev/null file
|
|
// open("/dev/null", O_RDWR);
|
|
// open("/dev/null", O_RDWR);
|
|
|
|
// Write the PID to the pidfile
|
|
FILE *fp;
|
|
fp = fopen(CREAT_PID_FILE_PATH, "w");
|
|
if (fp != NULL)
|
|
{
|
|
fprintf(fp, "%d\n", getpid());
|
|
fclose(fp);
|
|
}
|
|
else
|
|
{
|
|
fprintf(stdout, "create file error\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
fprintf(stdout,"server start...\n");
|
|
// Close all open file descriptors
|
|
int x;
|
|
for (x = sysconf(_SC_OPEN_MAX); x >= 0; x--)
|
|
{
|
|
close(x);
|
|
}
|
|
// Open the /dev/null file
|
|
open("/dev/null", O_RDWR);
|
|
open("/dev/null", O_RDWR);
|
|
// Now we can do the tasks of the daemon
|
|
while (1)
|
|
{
|
|
// Your daemon code here
|
|
sleep(1);
|
|
fprintf(stdout, "Daemon is running...\n");
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
const char *cmd = "mydaemon";
|
|
// Check for a valid command line argument
|
|
if (argc < 2)
|
|
{
|
|
fprintf(stderr, "Usage: %s start|stop\n", cmd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
// Check the command line argument
|
|
if (strcmp(argv[1], "start") == 0)
|
|
{
|
|
// 测试下是否已经有个进程了
|
|
int exit = access(CREAT_PID_FILE_PATH, F_OK);
|
|
if (exit == 0)
|
|
{
|
|
fprintf(stderr, "the server is exited, so safe exit\n");
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
fprintf(stdout, " try to start server\n");
|
|
}
|
|
daemonize(cmd);
|
|
}
|
|
else if (strcmp(argv[1], "stop") == 0)
|
|
{
|
|
// Code to stop the daemon
|
|
FILE *fp;
|
|
pid_t pid;
|
|
|
|
fp = fopen(CREAT_PID_FILE_PATH, "r");
|
|
if (fp != NULL)
|
|
{
|
|
|
|
fscanf(fp, "%d", &pid);
|
|
fclose(fp);
|
|
kill(pid, SIGTERM);
|
|
remove(CREAT_PID_FILE_PATH);
|
|
fprintf(stdout, "server stop...\n");
|
|
}
|
|
else
|
|
{
|
|
printf(" no find the process\n");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Usage: %s start|stop\n", cmd);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
} |