smarty template cache syncing daemon (PoC)
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.

200 lines
5.1KB

  1. /***************************************************************************
  2. * Copyright (C) 06/2006 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <getopt.h>
  25. #include <pwd.h>
  26. #include <sys/stat.h>
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include "configfile.h"
  31. #include "logging.h"
  32. #include "event.h"
  33. #include "multicast.h"
  34. #include "unixsock.h"
  35. #define DEFAULT_CONFIG "cachesyncd.conf"
  36. #define DEFAULT_LOGFILE "cachesyncd.log"
  37. #define DEFAULT_USER "httpd"
  38. #define BUF_SIZE 256
  39. static struct option opts[] = {
  40. {"config", 1, 0, 'c'},
  41. {"debug", 0, 0, 'd'},
  42. {"help", 0, 0, 'h'},
  43. {0, 0, 0, 0}
  44. };
  45. static int usock, msock;
  46. int usock_read_callback(int fd, void *privdata)
  47. {
  48. int len = read(fd, privdata, BUF_SIZE);
  49. if (len <= 0) {
  50. close(fd);
  51. return -1;
  52. }
  53. mcast_send(msock, privdata, len);
  54. return 0;
  55. }
  56. int usock_accept_callback(int fd, void *privdata)
  57. {
  58. int con = accept(usock, NULL, NULL);
  59. if (con < 0 ) {
  60. log_print(LOG_ERROR, "selector: accept()");
  61. return 0;
  62. }
  63. event_add_readfd(con, usock_read_callback, privdata);
  64. return 0;
  65. }
  66. int msock_read_callback(int fd, void *privdata)
  67. {
  68. int len = read(fd, privdata, BUF_SIZE);
  69. if (len <= 0) {
  70. log_print(LOG_ERROR, "selector: multicast sock closed?");
  71. } else if (!strncmp(privdata, "KEEPALIVE", 10)) {
  72. // nothing
  73. } else if (!strncmp(privdata, "DELETE ", 7)) {
  74. log_print(LOG_DEBUG, "delete '%s'", privdata +7);
  75. if (unlink(privdata +7))
  76. log_print(LOG_ERROR, "delete '%s' failed", privdata +7);
  77. } else {
  78. log_print(LOG_DEBUG, "recv unknown cmd via multicast: '%s'", privdata);
  79. }
  80. return 0;
  81. }
  82. int msock_keepalive_timeout(void *privdata)
  83. {
  84. mcast_send(msock, "KEEPALIVE", 10);
  85. return 0;
  86. }
  87. void drop_privileges(char *user)
  88. {
  89. struct passwd *user_info;
  90. user_info = getpwnam(user);
  91. if (user_info == NULL) {
  92. log_print(LOG_ERROR,"drop_privileges(): user '%s' not found", user);
  93. } else {
  94. if (setregid(user_info->pw_gid, user_info->pw_gid))
  95. log_print(LOG_ERROR, "setgid() failed");
  96. if (setreuid(user_info->pw_uid, user_info->pw_uid))
  97. log_print(LOG_ERROR, "setuid() failed");
  98. }
  99. }
  100. int main(int argc, char *argv[])
  101. {
  102. char *config = DEFAULT_CONFIG;
  103. int code, arg = 0, debug = 0;
  104. do {
  105. code = getopt_long(argc, argv, "c:dh", opts, &arg);
  106. switch (code) {
  107. case 'c': /* config */
  108. config = optarg;
  109. break;
  110. case 'd': /* debug */
  111. debug = 1;
  112. break;
  113. case 'h': /* help */
  114. printf("Usage: cachesyncd [options]\n"
  115. "Options: \n"
  116. " --config -c configfile use this configfile\n"
  117. " --debug -d do not fork and log to stderr\n"
  118. " --help -h this help\n"
  119. "\n");
  120. exit(0);
  121. break;
  122. case '?': /* error */
  123. exit(-1);
  124. break;
  125. default: /* unknown / all options parsed */
  126. break;
  127. }
  128. } while (code != -1);
  129. /* parse config file */
  130. if (!config_parse(config))
  131. exit(-1);
  132. /* check logfile */
  133. char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
  134. if (logfile != NULL && debug == 0) {
  135. /* start logging */
  136. if (!log_init(logfile))
  137. exit(-1);
  138. /* zum daemon mutieren */
  139. daemon(-1, 0);
  140. }
  141. log_print(LOG_EVERYTIME, "cachesyncd started (pid: %d)", getpid());
  142. char *buf = malloc(BUF_SIZE);
  143. if (buf == NULL) {
  144. log_print(LOG_ERROR, "selector: out of memory");
  145. return -1;
  146. }
  147. msock = mcast_init();
  148. if (msock < 0)
  149. return -1;
  150. drop_privileges(config_get_string("global", "user", DEFAULT_USER));
  151. usock = sock_init();
  152. if (usock < 0)
  153. return -1;
  154. event_add_readfd(usock, usock_accept_callback, buf);
  155. event_add_readfd(msock, msock_read_callback, buf);
  156. struct timeval tv;
  157. tv.tv_sec = 60;
  158. tv.tv_usec = 0;
  159. event_add_timeout(&tv, msock_keepalive_timeout, NULL);
  160. event_loop();
  161. return 0;
  162. }