Guest User

Untitled

a guest
Dec 22nd, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/wait.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <signal.h>
  14. int childPID;
  15. void restartApp(int) {
  16. if(childPID) {
  17. int statval;
  18. kill(childPID, SIGINT);
  19. wait(&statval);
  20. printf("Restarting...\n");
  21. system("./rm");
  22. exit(0);
  23. } else {
  24. kill(getppid(), SIGUSR1);
  25. }
  26. }
  27. void inthandler(int) {
  28. printf("Stopping in 60 seconds...\n");
  29. sleep(60);
  30. printf("Shutting down\n");
  31. exit(0);
  32. }
  33. auto main(int argc, char ** argv) -> int {
  34. signal(SIGHUP, SIG_IGN);
  35. signal(SIGUSR1, &restartApp);
  36. struct stat buf;
  37. stat("rm", &buf);
  38. time_t last_mod=buf.st_mtim.tv_sec;
  39. int s, s2, len;
  40. unsigned int t;
  41. struct sockaddr_un local,remote;
  42. char str[1024];
  43. printf("%d: Starting server\n", getpid());
  44. if((childPID=fork()) == 0)
  45. {
  46. signal(SIGINT, &inthandler);
  47. if((s=socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
  48. perror("socket");
  49. exit(1);
  50. }
  51. local.sun_family=AF_UNIX;
  52. strcpy(local.sun_path, "rmsock");
  53. unlink(local.sun_path);
  54. len = strlen(local.sun_path) + sizeof(local.sun_family);
  55. if(bind(s, (struct sockaddr*)&local, len) == -1) {
  56. perror("bind");
  57. exit(1);
  58. }
  59. if(listen(s,100) == -1) {
  60. perror("listen");
  61. exit(1);
  62. }
  63. for(;;) {
  64. int done, n;
  65. printf("Waiting for a connection...\n");
  66. t=sizeof(remote);
  67. if((s2=accept(s, (struct sockaddr *)&remote, &t)) == -1) {
  68. perror("accept");
  69. exit(1);
  70. }
  71. printf("Connected.\n");
  72. done=0;
  73. do {
  74. n=recv(s2, str, 1024, 0);
  75. if(n<=0) {
  76. if(n<0) perror("recv");
  77. done=1;
  78. }
  79. if(!done) {
  80. if(!strcmp(str,"hello\n")) {
  81. send(s2, "Auth required...\n", 17, 0);
  82. } else
  83. if(send(s2, "Unknown command\n", 17, 0) < 0) {
  84. perror("send");
  85. done=1;
  86. }
  87. }
  88. } while(!done);
  89. close(s2);
  90. }
  91. }
  92. else
  93. {
  94. for(;;) {
  95. stat("rm",&buf);
  96. if(buf.st_mtim.tv_sec!=last_mod) {
  97. kill(getpid(),SIGUSR1);
  98. }
  99. sleep(1);
  100. }
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment