| 1 | #include <unistd.h> |
| 2 | #include <getopt.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <signal.h> |
| 8 | #include <errno.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <sys/un.h> |
| 11 | |
| 12 | #define UNIX_PATH_MAX 108 |
| 13 | #define SOCK_DIR "/tmp/socks" |
| 14 | #define UST_SIGNAL SIGIO |
| 15 | |
| 16 | struct ust_msg { |
| 17 | char *raw; |
| 18 | }; |
| 19 | |
| 20 | void parse_opts(int argc, char **argv) |
| 21 | { |
| 22 | int flags, opt; |
| 23 | int nsecs, tfnd; |
| 24 | |
| 25 | nsecs = 0; |
| 26 | tfnd = 0; |
| 27 | flags = 0; |
| 28 | while ((opt = getopt(argc, argv, "nt:")) != -1) { |
| 29 | switch (opt) { |
| 30 | case 'n': |
| 31 | flags = 1; |
| 32 | break; |
| 33 | case 't': |
| 34 | nsecs = atoi(optarg); |
| 35 | tfnd = 1; |
| 36 | break; |
| 37 | default: /* '?' */ |
| 38 | fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n", |
| 39 | argv[0]); |
| 40 | exit(EXIT_FAILURE); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind); |
| 45 | |
| 46 | if (optind >= argc) { |
| 47 | fprintf(stderr, "Expected argument after options\n"); |
| 48 | exit(EXIT_FAILURE); |
| 49 | } |
| 50 | |
| 51 | printf("name argument = %s\n", argv[optind]); |
| 52 | |
| 53 | /* Other code omitted */ |
| 54 | |
| 55 | exit(EXIT_SUCCESS); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | void signal_process(pid_t pid) |
| 60 | { |
| 61 | int result; |
| 62 | |
| 63 | result = kill(pid, UST_SIGNAL); |
| 64 | if(result == -1) { |
| 65 | perror("kill"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | sleep(1); |
| 70 | } |
| 71 | |
| 72 | int send_message(pid_t pid, const char *msg) |
| 73 | { |
| 74 | int fd; |
| 75 | int result; |
| 76 | struct sockaddr_un addr; |
| 77 | |
| 78 | result = fd = socket(PF_UNIX, SOCK_DGRAM, 0); |
| 79 | if(result == -1) { |
| 80 | perror("socket"); |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | addr.sun_family = AF_UNIX; |
| 85 | |
| 86 | result = snprintf(addr.sun_path, UNIX_PATH_MAX, "%s/%d", SOCK_DIR, pid); |
| 87 | if(result >= UNIX_PATH_MAX) { |
| 88 | fprintf(stderr, "string overflow allocating socket name"); |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | char *buf; |
| 93 | |
| 94 | asprintf(&buf, "%s\n", msg); |
| 95 | |
| 96 | signal_process(pid); |
| 97 | |
| 98 | result = sendto(fd, buf, strlen(buf), 0, (struct sockaddr *)&addr, sizeof(addr)); |
| 99 | if(result == -1) { |
| 100 | perror("sendto"); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | free(buf); |
| 105 | |
| 106 | // result = fd = open(sockfile, O_RDWR); |
| 107 | // if(result == -1 && errno == ENXIO) { |
| 108 | // fprintf(stderr, "signalling process\n"); |
| 109 | // |
| 110 | // result = fd = open(sockfile, O_RDWR); |
| 111 | // if(result == -1) { |
| 112 | // perror("open"); |
| 113 | // return 1; |
| 114 | // } |
| 115 | // } |
| 116 | // else if(result == -1) { |
| 117 | // perror("open"); |
| 118 | // return 1; |
| 119 | // } |
| 120 | |
| 121 | } |
| 122 | |
| 123 | int main(int argc, char *argv[]) |
| 124 | { |
| 125 | pid_t pid = atoi(argv[1]); |
| 126 | |
| 127 | char *msg = argv[2]; |
| 128 | |
| 129 | send_message(pid, msg); |
| 130 | |
| 131 | return 0; |
| 132 | } |