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.

67 lines
1.5 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include "utils.h"
  5. void FATAL(const char *msg) {
  6. fprintf(stderr, "%s", msg);
  7. exit(-1);
  8. }
  9. void usage() {
  10. printf("usage: ss -s server_host -p server_port -l local_port\n");
  11. printf(" -k password [-m encrypt_method] [-f pid_file]\n");
  12. printf("\n");
  13. printf("options:\n");
  14. printf(" encrypt_method: table, rc4\n");
  15. printf(" pid_file: valid path to the pid file\n");
  16. }
  17. void demonize(const char* path) {
  18. /* Our process ID and Session ID */
  19. pid_t pid, sid;
  20. /* Fork off the parent process */
  21. pid = fork();
  22. if (pid < 0) {
  23. exit(EXIT_FAILURE);
  24. }
  25. /* If we got a good PID, then
  26. we can exit the parent process. */
  27. if (pid > 0) {
  28. FILE *file = fopen(path, "w");
  29. if (file == NULL) FATAL("Invalid pid file\n");
  30. fprintf(file, "%d", pid);
  31. fclose(file);
  32. exit(EXIT_SUCCESS);
  33. }
  34. /* Change the file mode mask */
  35. umask(0);
  36. /* Open any logs here */
  37. /* Create a new SID for the child process */
  38. sid = setsid();
  39. if (sid < 0) {
  40. /* Log the failure */
  41. exit(EXIT_FAILURE);
  42. }
  43. /* Change the current working directory */
  44. if ((chdir("/")) < 0) {
  45. /* Log the failure */
  46. exit(EXIT_FAILURE);
  47. }
  48. /* Close out the standard file descriptors */
  49. close(STDIN_FILENO);
  50. close(STDOUT_FILENO);
  51. close(STDERR_FILENO);
  52. }