Created
January 7, 2016 15:03
-
-
Save steelman/334577de69ecd252d8f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <sys/un.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/select.h> | |
int main(int ac, char* av[]) | |
{ | |
char buf[128]; | |
int fd; | |
fd_set socks; | |
char listen_pid[64]; | |
char* listen_fds="LISTEN_FDS=1"; | |
struct sockaddr_un xsockaddr; | |
if (ac < 3) { | |
fprintf(stderr, "Usage:\n on-demand <socket> <command>\n"); | |
exit(1); | |
} | |
snprintf(listen_pid, sizeof(listen_pid), "LISTEN_PID=%d", getpid()); | |
putenv(listen_fds); | |
putenv(listen_pid); | |
memset(buf, 0, sizeof(buf)); | |
fd=socket(AF_UNIX, SOCK_STREAM, 0); | |
if (fd < 0) { | |
perror("socket"); | |
exit(1); | |
} | |
printf("socket: %d\n", fd); | |
memset(&xsockaddr, 0, sizeof(xsockaddr)); | |
xsockaddr.sun_family = AF_UNIX; | |
snprintf(xsockaddr.sun_path, | |
sizeof(xsockaddr.sun_path), | |
"%s", av[1]); | |
unlink(xsockaddr.sun_path); | |
if (bind(fd, (struct sockaddr *) &xsockaddr, sizeof(xsockaddr)) == -1) { | |
perror("bind"); | |
exit(1); | |
} | |
if (listen(fd, 16) == -1) { | |
perror("listen"); | |
exit(1); | |
} | |
FD_ZERO(&socks); | |
FD_SET(fd, &socks); | |
select(fd+1, &socks, NULL, NULL, NULL); | |
execvp(av[2], av+2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment