| 1 | #include <sys/types.h> |
| 2 | #include <signal.h> |
| 3 | #include <errno.h> |
| 4 | #include <sys/socket.h> |
| 5 | #include <sys/un.h> |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | #define UNIX_PATH_MAX 108 |
| 11 | #define SOCK_DIR "/tmp/socks" |
| 12 | #define UST_SIGNAL SIGIO |
| 13 | |
| 14 | static void signal_process(pid_t pid) |
| 15 | { |
| 16 | int result; |
| 17 | |
| 18 | result = kill(pid, UST_SIGNAL); |
| 19 | if(result == -1) { |
| 20 | perror("kill"); |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | sleep(1); |
| 25 | } |
| 26 | |
| 27 | int send_message(pid_t pid, const char *msg, const char *reply) |
| 28 | { |
| 29 | int fd; |
| 30 | int result; |
| 31 | struct sockaddr_un addr; |
| 32 | char *buf; |
| 33 | |
| 34 | result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 35 | if(result == -1) { |
| 36 | perror("socket"); |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | addr.sun_family = AF_UNIX; |
| 41 | |
| 42 | result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); |
| 43 | if(result >= UNIX_PATH_MAX) { |
| 44 | fprintf(stderr, "string overflow allocating socket name"); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | asprintf(&buf, "%s\n", msg); |
| 49 | |
| 50 | signal_process(pid); |
| 51 | |
| 52 | result = sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); |
| 53 | if(result == -1) { |
| 54 | perror("sendto"); |
| 55 | return 1; |
| 56 | } |
| 57 | |
| 58 | free(buf); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | |