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.
 
 
 
 
 
 

95 lines
2.2 KiB

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "utils.h"
#define INT_DIGITS 19 /* enough for 64 bit integer */
char *itoa(int i) {
/* Room for INT_DIGITS digits, - and '\0' */
static char buf[INT_DIGITS + 2];
char *p = buf + INT_DIGITS + 1; /* points to terminating '\0' */
if (i >= 0) {
do {
*--p = '0' + (i % 10);
i /= 10;
} while (i != 0);
return p;
}
else { /* i < 0 */
do {
*--p = '0' - (i % 10);
i /= 10;
} while (i != 0);
*--p = '-';
}
return p;
}
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);
}