Open System Services Programmer's Guide
/* get process handle and cpu number */
PROCESSHANDLE_GETMINE_(phandle);
PROCESSHANDLE_DECOMPOSE_(phandle, &cpu);
/* print startup message */
fprintf(stderr, "slaunch1 starting in cpu %d\n", cpu);
/* create AF_INET stream socket */
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "socket() call failed [errno %d]\n", errno);
exit(1);
}
/* Get server port from the command line or use default 12345 */
if (argc > 1)
addr.sin_port = (short)atoi(argv[1]); /* use command line arg */
else
addr.sin_port = 12345; /* use default */
addr.sin_family = AF_INET; /* internet address family */
addr.sin_addr.s_addr = gethostid(); /* assume server on our host */
/* bind socket to address */
if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0)
{
fprintf(stderr, "bind() call failed [errno %d]\n", errno);
exit(1);
}
/* make the socket a listening socket */
if (listen(fd, 5) < 0)
{
fprintf(stderr, "listen() call failed [errno %d]\n", errno);
exit(1);
}
listenfd = fd;
fcntl(listenfd, F_SETFD, FD_CLOEXEC); /* don't propagate fd on spawn */
FD_ZERO(&fdset);
/* fd usage
* 2 stderr - used by slaunch1 for messages,
* inherited by sserver
* 3 listenfd - used by slaunch1 for connections, not inherited by sserver
* y fd - accepted connection, passed to sserver and closed
* x serverfd[cpu] - slaunch1 fd corresponding to sserver stdin
*/
/* spawn a static server on each available cpu */
for (cpuindex = 0; cpuindex < cpucount; cpuindex++)
{
if ((cpumask >> (15-cpuindex)) & 1)
{
/* use AF_UNIX socket pair for slaunch1/sserver fd passing */
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pairfd) < 0)
{
fprintf(stderr, "socketpair() call failed [errno %d]\n",
errno);
exit(1);
}
/* setup stdin and stdout */
dup2(pairfd[1],STDIN_FILENO); /* setup server stdin */
dup2(pairfd[1],STDOUT_FILENO); /* setup server stdout */
close(pairfd[1]);
serverfd[cpuindex] = pairfd[0];
fcntl(pairfd[0], F_SETFD, FD_CLOEXEC); /* don't propagate fd on spawn */
inherit.flags = 0;
peparm.pe_cpu = cpuindex;
144 Managing Processes