Advertisement
Guest User

Untitled

a guest
Aug 12th, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <sys/un.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stddef.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9.  
  10. int notify_ready()
  11. {
  12.   int fd = -1;
  13.   struct sockaddr_un *addr = NULL;
  14.   socklen_t addrlen;
  15.   const char *notify_socket = getenv("NOTIFY_SOCKET");
  16.   const char *message = "READY=1";
  17.   ssize_t l;
  18.  
  19.   if (!notify_socket)
  20.     return 0;
  21.  
  22.   addrlen = (socklen_t) (offsetof(struct sockaddr_un, sun_path)
  23.              + strlen(notify_socket) + 1);
  24.  
  25.   fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  26.   if (fd < 0)
  27.     return -errno;
  28.  
  29.   addr = (struct sockaddr_un *) calloc(1, (size_t) addrlen);
  30.   if (!addr) {
  31.     int r = -errno;
  32.     close(fd);
  33.     return r;
  34.   }
  35.  
  36.   addr->sun_family = AF_UNIX;
  37.   strcpy(addr->sun_path, notify_socket);
  38.   if (addr->sun_path[0] == '@') {
  39.     addr->sun_path[0] = '\0';
  40.     --addrlen;
  41.   }
  42.  
  43.   l = sendto(fd, message, (size_t) strlen(message), 0,
  44.         (const struct sockaddr *) addr, addrlen);
  45.   if (l < 0) {
  46.     int r = -errno;
  47.     close(fd);
  48.     free(addr);
  49.     return r;
  50.   }
  51.  
  52.   free(addr);
  53.  
  54.   return 1;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement