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.

unixsock.c 2.6KB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <unistd.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <sys/stat.h>
  26. #include "configfile.h"
  27. #include "logging.h"
  28. #define DEFAULT_SOCKET "/tmp/cachesyncd"
  29. int sock_init()
  30. {
  31. struct sockaddr_un addr;
  32. int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  33. if (sockfd == -1) {
  34. log_print(LOG_ERROR, "unixsock: socket()");
  35. return -1;
  36. }
  37. char *filename = config_get_string("global", "socket", DEFAULT_SOCKET);
  38. addr.sun_family = AF_UNIX;
  39. strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
  40. int len = sizeof(addr.sun_family) + strlen(addr.sun_path);
  41. mode_t old_umask = umask(0077);
  42. int ret = bind(sockfd, (struct sockaddr *) &addr, len);
  43. umask(old_umask);
  44. if (ret == -1) {
  45. log_print(LOG_ERROR, "unixsock: bind()");
  46. close(sockfd);
  47. return -1;
  48. }
  49. ret = listen(sockfd, 5);
  50. if (ret == -1) {
  51. log_print(LOG_ERROR, "unixsock: listen()");
  52. close(sockfd);
  53. return -1;
  54. }
  55. return sockfd;
  56. }
  57. void sock_close(int sockfd)
  58. {
  59. char *filename;
  60. close(sockfd);
  61. filename = config_get_string("global", "socket", DEFAULT_SOCKET);
  62. if (unlink(filename) == -1)
  63. log_print(LOG_WARN, "unixsock: unlink()");
  64. }