You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.7 KiB
71 lines
1.7 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#include "utils.h"
|
|
|
|
void FATAL(const char *msg) {
|
|
fprintf(stderr, "%s", msg);
|
|
exit(-1);
|
|
}
|
|
|
|
void usage() {
|
|
printf("usage:\n\n");
|
|
printf(" ss -s server_host -p server_port -l local_port -k password\n");
|
|
printf(" [-m encrypt_method] [-f pid_file] [-t timeout] [-c config_file]\n");
|
|
printf("\n");
|
|
printf("options:\n\n");
|
|
printf(" encrypt_method: table, rc4\n");
|
|
printf(" pid_file: valid path to the pid file\n");
|
|
printf(" timeout: socket timeout in senconds\n");
|
|
printf(" config_file: json format config file\n");
|
|
printf("\n\n");
|
|
}
|
|
|
|
void demonize(const char* path) {
|
|
|
|
/* Our process ID and Session ID */
|
|
pid_t pid, sid;
|
|
|
|
/* Fork off the parent process */
|
|
pid = fork();
|
|
if (pid < 0) {
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/* If we got a good PID, then
|
|
we can exit the parent process. */
|
|
if (pid > 0) {
|
|
FILE *file = fopen(path, "w");
|
|
if (file == NULL) FATAL("Invalid pid file\n");
|
|
|
|
fprintf(file, "%d", pid);
|
|
fclose(file);
|
|
exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
/* Change the file mode mask */
|
|
umask(0);
|
|
|
|
/* Open any logs here */
|
|
|
|
/* Create a new SID for the child process */
|
|
sid = setsid();
|
|
if (sid < 0) {
|
|
/* Log the failure */
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/* Change the current working directory */
|
|
if ((chdir("/")) < 0) {
|
|
/* Log the failure */
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/* Close out the standard file descriptors */
|
|
close(STDIN_FILENO);
|
|
close(STDOUT_FILENO);
|
|
close(STDERR_FILENO);
|
|
|
|
}
|
|
|