Run SS as Daemon

/**
 *
 * @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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值