Skip to content

Instantly share code, notes, and snippets.

@AdamMagaluk
Created October 17, 2012 21:25
Show Gist options
  • Save AdamMagaluk/3908311 to your computer and use it in GitHub Desktop.
Save AdamMagaluk/3908311 to your computer and use it in GitHub Desktop.
libwebsockets - libwebsockets_fork_service_loop
int fd;
struct sockaddr_in cli_addr;
int n;
int p;
n = fork();
if (n < 0)
return n;
if (!n) {
/* main process context */
/*
* set up the proxy sockets to allow broadcast from
* service process context
*/
for (p = 0; p < context->count_protocols; p++) {
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "Unable to create socket\n");
return -1;
}
cli_addr.sin_family = AF_INET;
cli_addr.sin_port = htons(
context->protocols[p].broadcast_socket_port);
cli_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
n = connect(fd, (struct sockaddr *)&cli_addr,
sizeof cli_addr);
if (n < 0) {
fprintf(stderr, "Unable to connect to "
"broadcast socket %d, %s\n",
n, strerror(errno));
return -1;
}
context->protocols[p].broadcast_socket_user_fd = fd;
}
return 0;
}
#ifdef HAVE_SYS_PRCTL_H
/* we want a SIGHUP when our parent goes down */
prctl(PR_SET_PDEATHSIG, SIGHUP);
#endif
/* in this forked process, sit and service websocket connections */
while (1) {
if (libwebsocket_service(context, 1000))
break;
#ifndef HAVE_SYS_PRCTL_H
/*
* on systems without prctl() (i.e. anything but linux) we can notice that our
* parent is dead if getppid() returns 1. FIXME apparently this is not true for
* solaris, could remember ppid right after fork and wait for it to change.
*/
if (getppid() == 1)
break;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment