rats

RAdix iniT Suite
git clone https://git.daat.foo/repos/rats.git
Log | Files | Refs | README | LICENSE

control.c (1340B)


      1 #ifndef _POSIX_C_SOURCE
      2 #define _POSIX_C_SOURCE 200809L
      3 #endif
      4 
      5 #include <errno.h>
      6 #include <fcntl.h>
      7 #include <signal.h>
      8 #include <stdio.h>
      9 #include <string.h>
     10 #include <sys/socket.h>
     11 #include <sys/stat.h>
     12 #include <sys/un.h>
     13 #include <unistd.h>
     14 
     15 int
     16 main(int argc, char *argv[])
     17 {
     18 	int fd;
     19 	struct sockaddr_un addr;
     20 	struct stat st;
     21 	struct sigaction sa;
     22 
     23 	if (argc == 1) {
     24 		for (fd = 3; fd < 256; fd++) {
     25 			errno = 0;
     26 			if (fcntl(fd, F_GETFD) >= 0 || errno != EBADF) {
     27 				printf("inherited descriptor: %d\n", fd);
     28 				return 1;
     29 			}
     30 		}
     31 		if (sigaction(SIGPIPE, NULL, &sa) < 0 || sa.sa_handler != SIG_DFL) {
     32 			puts("SIGPIPE disposition was not restored");
     33 			return 1;
     34 		}
     35 		puts("OK: no supervisor descriptors inherited");
     36 		return 0;
     37 	}
     38 	if (argc != 2)
     39 		return 1;
     40 	if (strcmp(argv[1], "-mode") == 0)
     41 		return stat(SOCK_PATH, &st) < 0 || (st.st_mode & 0777) != 0600;
     42 	if (strcmp(argv[1], "-disconnect") != 0)
     43 		return 1;
     44 
     45 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
     46 	if (fd < 0)
     47 		return 1;
     48 	memset(&addr, 0, sizeof(addr));
     49 	addr.sun_family = AF_UNIX;
     50 	if (strlen(SOCK_PATH) >= sizeof(addr.sun_path))
     51 		return 1;
     52 	memcpy(addr.sun_path, SOCK_PATH, sizeof(SOCK_PATH));
     53 	if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
     54 		return 1;
     55 	if (write(fd, "s\n", 2) != 2)
     56 		return 1;
     57 	shutdown(fd, SHUT_RDWR);
     58 	close(fd);
     59 	return 0;
     60 }