f.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <errno.h>
  4. #include <sys/stat.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <syslog.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. int main(int argc, char *argv[])
  11. {
  12. /* Open a connection to the syslog server */
  13. openlog(argv[0],LOG_NOWAIT|LOG_PID,LOG_USER);
  14. /* Fork off the parent process */
  15. pid_t pid;
  16. pid = fork();
  17. /* If the pid is less than zero, something went wrong when forking */
  18. if (pid < 0)
  19. {
  20. /* Now we will tel the log server cant fork*/
  21. syslog(LOG_NOTICE, "Cant Fork\n");
  22. exit(EXIT_FAILURE);
  23. }
  24. /* If the pid we got back was greater than zero, then the clone was successful and we are the parent. */
  25. if (pid > 0)
  26. {
  27. exit(EXIT_SUCCESS);
  28. }
  29. /* If execution reaches this point we are the child */
  30. /* Creating a Unique Session ID (SID)*/
  31. if (setsid() < 0)
  32. syslog(LOG_NOTICE, "I cant Creat a Unique Session ID ");
  33. exit(EXIT_FAILURE);
  34. //catch/ignore signals
  35. signal(SIGCHLD,SIG_IGN);
  36. signal(SIGHUP,SIG_IGN);
  37. /* Fork off the parent process */
  38. pid_t pid1;
  39. pid1 = fork();
  40. /* If the pid1 is less than zero, something went wrong when forking */
  41. if (pid1 < 0)
  42. {
  43. /* Now we will tel the log server cant fork*/
  44. syslog(LOG_NOTICE, "Cant Fork\n");
  45. exit(EXIT_FAILURE);
  46. }
  47. /* If the pid1 we got back was greater than zero, then the clone was successful and we are the parent. */
  48. if (pid1 > 0)
  49. {
  50. exit(EXIT_SUCCESS);
  51. }
  52. /* Now we will Set the umask to zero */
  53. umask(0);
  54. /* its seme ok now*/
  55. /* Now we will Change the current working directory to root */
  56. chdir("/");
  57. /* Close all Files */
  58. close(STDIN_FILENO);
  59. close(STDOUT_FILENO);
  60. close(STDERR_FILENO);
  61. /* Now we will tel the log server its ok now */
  62. syslog(LOG_NOTICE, "Successfully started daemon\n");
  63. /* close a connection to the syslog server */
  64. closelog();
  65. /* make new file to save last commit */
  66. FILE *fp = fopen("scores.dat", "ab+");
  67. fclose(fp);
  68. while (1)
  69. {
  70. system("bash /home/clara/fun/run.sh");
  71. }
  72. }