Open System Services Programmer's Guide
Example 26 Dynamic Launcher Program
/* dlaunch1.c
*
* Simple OSS example using OSS socket APIs for communication
* between requester and server.
*
* dlaunch1 (dynamic launcher 1) is an inetd-like program which
* accepts socket connections and exec's a dserver process for
* each connection.
*
* dserver reads stdin and echoes to stdout. dlaunch1 and dserver
* write progress messages and error messages to stderr.
*
* dlaunch1 and the multiple dserver processes run on the same cpu.
*/
#define _XOPEN_SOURCE_EXTENDED 1 /* needed for OSS sockets */
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netdb.h>
/* Guardian APIs */
#include <cextdecs.h(PROCESSHANDLE_GETMINE_, PROCESSHANDLE_DECOMPOSE_)>
short cpu, phandle[10]; /* our cpu and process handle */
int newpid, fd, listenfd; /* process id, file descriptor, listening fd */
int readycount, bytesread; /* ready fd count, recv byte count */
struct sockaddr_in addr; /* socket address (family,addr,port) */
struct timeval timeval; /* timeout structure for select() call */
fd_set fdset; /* set of fds for select() call */
#define IDLE_PERIOD 180 /* 180 second timeout value */
int main(int argc, char **argv)
{
/* get process handle and cpu number */
PROCESSHANDLE_GETMINE_(phandle);
PROCESSHANDLE_DECOMPOSE_(phandle, &cpu);
/* print startup message */
fprintf(stderr, "dlaunch1 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 */
Common and Unique Characteristics 107










