/**
*
* @File: sslocal.c
* @Date: Sunday, 9 October 2016
*
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
#include <syslog.h>
static void skeleton_daemon()
{
pid_t pid;
/* Fork off the parent process */
pid = fork();
/* An error occurred */
if (pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
/* Catch, ignore and handle signals */
//TODO: Implement a working signal handler */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Fork off for the second time*/
pid = fork();
/* An error occurred */
if (pid < 0)
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 */
chdir("/");
/* Close all open file descriptors */
#if 0
int x;
for (x = sysconf(_SC_OPEN_MAX); x>0; x--)
{
close (x);
}
/* Open the log file */
openlog (NULL, LOG_PID, LOG_DAEMON);
#endif
}
int main(int argc, char* argv[]){
char cmd[512];
memset(cmd, '\0', 512);
strcat(cmd, "shadowsocks-local");
int opt;
while((opt = getopt(argc, argv, "dAs:k:m:p:t:b:l:c:")) != -1){
switch(opt){
case 'd':
strcat(cmd, " -d");
break;
case 'A':
strcat(cmd, " -A");
break;
case 's':
strcat(cmd, " -s ");
strcat(cmd, optarg);
break;
case 'k':
strcat(cmd, " -k ");
strcat(cmd, optarg);
break;
case 'm':
strcat(cmd, " -m ");
strcat(cmd, optarg);
break;
case 'p':
strcat(cmd, " -p ");
strcat(cmd, optarg);
break;
case 't':
strcat(cmd, " -t ");
strcat (cmd, optarg);
break;
case 'b':
strcat(cmd, " -b ");
strcat(cmd, optarg);
break;
case 'l':
strcat(cmd, " -l ");
strcat(cmd, optarg);
break;
case 'c':
strcat(cmd, " -c ");
strcat(cmd, optarg);
break;
default:
system("shadowsocks-local -version");
system("shadowsocks-local --help");
exit(0);
}
}
skeleton_daemon();
system(cmd);
exit(0);
}
Run SS as Daemon
最新推荐文章于 2022-02-23 15:02:25 发布