home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / gnuserv / gnuserv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-23  |  13.3 KB  |  543 lines

  1. /* -*-C-*-
  2.  Server code for handling requests from clients and forwarding them
  3.  on to the GNU Emacs process.
  4.  
  5.  This file is part of GNU Emacs.
  6.  
  7.  Copying is permitted under those conditions described by the GNU
  8.  General Public License.
  9.  
  10.  Copyright (C) 1989 Free Software Foundation, Inc.
  11.  
  12.  Author: Andy Norman (ange@hplb.hpl.hp.com), based on 'etc/server.c'
  13.          from the 18.52 GNU Emacs distribution.
  14.  
  15.  Please mail bugs and suggestions to the author at the above address.
  16. */
  17.  
  18. static char rcsid [] = "$Header: gnuserv.c,v 1.8 89/09/28 12:47:01 ange Exp $";
  19.  
  20. #include "gnuserv.h"
  21.  
  22. #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS)
  23. main ()
  24. {
  25.   fprintf (stderr,"Sorry, the Emacs server is only supported on systems that have\n");
  26.   fprintf (stderr,"Unix Domain sockets, Internet Domain sockets or System V IPC\n");
  27.   exit (1);
  28. } /* main */
  29. #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  30.  
  31. #ifdef SYSV_IPC
  32.  
  33. int ipc_qid = 0;        /* ipc message queue id */
  34. int ipc_wpid = 0;        /* watchdog task pid */
  35.  
  36.  
  37. /*
  38.   ipc_exit -- clean up the queue id and queue, then kill the watchdog task
  39.               if it exists. exit with the given status.
  40. */
  41. void ipc_exit(stat)
  42.      int stat;
  43. {
  44.   msgctl(ipc_qid,IPC_RMID,0);
  45.   
  46.   if (ipc_wpid != 0)
  47.     kill(ipc_wpid,SIGKILL);
  48.  
  49.   exit(stat);
  50. } /* ipc_exit */
  51.  
  52.  
  53. /*
  54.   ipc_handle_signal -- catch the signal given and clean up.
  55. */
  56. void ipc_handle_signal(sig)
  57.      int sig;
  58. {
  59.   ipc_exit(0);
  60. } /* ipc_handle_signal */
  61.  
  62.  
  63. /* 
  64.   ipc_spawn_watchdog -- spawn a watchdog task to clean up the message queue should the
  65.             server process die.
  66. */
  67. int ipc_spawn_watchdog()
  68. {
  69.   if ((ipc_wpid = fork()) == 0) { /* child process */
  70.     int ppid = getppid();    /* parent's process id */
  71.  
  72.     setpgrp();            /* gnu kills process group on exit */
  73.     
  74.     while (1) {
  75.       if (kill(ppid,0) < 0) {    /* ppid is no longer valid, parent may have died */
  76.     ipc_exit(0);
  77.       }; /* if */
  78.  
  79.       sleep(10);        /* have another go later */
  80.     }; /* while */
  81.   }; /* if */
  82.  
  83. } /* ipc_spawn_watchdog */
  84.  
  85.  
  86. /*
  87.   ipc_init -- initialize server, setting the global msqid that can be listened on.
  88. */
  89. void ipc_init(msgpp)
  90.      struct msgbuf **msgpp;
  91. {
  92.   key_t key;            /* messge key */
  93.   char buf[BUFSIZ];        /* pathname for key */
  94.   int p;            /* child process id */
  95.  
  96.   sprintf(buf,"/tmp/gsrv%d",geteuid());
  97.   creat(buf,0600);
  98.   key = ftok(buf,1);
  99.  
  100.   if ((ipc_qid = msgget(key,0600|IPC_CREAT)) == -1) {
  101.     perror(progname);
  102.     fprintf(stderr,"%s: unable to create msg queue\n",progname);
  103.     ipc_exit(1);
  104.   }; /* if */
  105.  
  106.   ipc_spawn_watchdog();
  107.  
  108.   signal(SIGTERM,ipc_handle_signal);
  109.   signal(SIGINT,ipc_handle_signal);
  110.  
  111.   if ((*msgpp = (struct msgbuf *) malloc(sizeof **msgpp + BUFSIZ)) == NULL) {
  112.     fprintf(stderr,"%s: unable to allocate space for message buffer\n",progname);
  113.     ipc_exit(1);
  114.   }; /* if */
  115.  
  116. } /* ipc_init */
  117.  
  118.  
  119. /*
  120.   handle_ipc_request -- accept a request from a client, pass the request on
  121.               to the GNU Emacs process, then wait for its reply and
  122.             pass that on to the client.
  123. */
  124. void handle_ipc_request(msgp)
  125.      struct msgbuf *msgp;    /* message buffer */
  126. {
  127.   struct msqid_ds msg_st;    /* message status */
  128.   char buf[BUFSIZ];
  129.   int len;            /* length of message / read */
  130.   int junk;            /* junk value */
  131.  
  132.   if ((len = msgrcv(ipc_qid,msgp,BUFSIZ-1,1,0)) < 0) {
  133.     perror(progname);
  134.     fprintf(stderr,"%s: unable to receive\n",progname);
  135.     ipc_exit(1);
  136.   }; /* if */
  137.  
  138.   msgctl(ipc_qid,IPC_STAT,&msg_st);
  139.   strncpy(buf,msgp->mtext,len);
  140.   buf[len] = '\0';        /* terminate */
  141.   
  142.   printf("%d %s",ipc_qid,buf);
  143.   fflush(stdout);
  144.  
  145.   if ((len = read(0,buf,BUFSIZ)) < 0) {
  146.     perror(progname);
  147.     fprintf(stderr,"%s: unable to read\n",progname);
  148.     ipc_exit(1);
  149.   }; /* if */
  150.       
  151.   /* parse the response from gnu */
  152.   msgp->mtext[0] = '\0';
  153.   sscanf(buf,"%d:%[^\n]\n",&junk,msgp->mtext);
  154.  
  155.   /* Send a response back to the client. */
  156.   msgp->mtype = msg_st.msg_lspid;
  157.   msgsnd(ipc_qid,msgp,strlen(msgp->mtext)+1,0);
  158.  
  159. } /* handle_ipc_request */
  160. #endif /* SYSV_IPC */
  161.  
  162.  
  163. #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
  164. /*
  165.   echo_request -- read request from a given socket descriptor, and send the information
  166.                   to stdout (the gnu process).
  167. */
  168. void echo_request(s)
  169. int s;                /* socket */
  170. {
  171.   char buf[BUFSIZ];
  172.   int len;
  173.  
  174.   printf("%d ",s);
  175.   
  176.   /* read until we get a newline or no characters */
  177.   while ((len = recv(s,buf,BUFSIZ,0)) > 0) {
  178.     buf[len] = '\0';
  179.     printf("%s",buf);
  180.  
  181.     if (buf[len-1] == '\n')
  182.       break;            /* end of message */
  183.  
  184.   }; /* while */
  185.  
  186.   if (len < 0) {
  187.     perror(progname);
  188.     fprintf(stderr,"%s: unable to recv\n",progname);
  189.     exit(1);
  190.   }; /* if */
  191.   
  192. } /* echo_request */
  193.  
  194.  
  195. /*
  196.   handle_response -- accept a response from stdin (the gnu process) and pass the
  197.                      information on to the relevant client.
  198. */
  199. void handle_response()
  200. {
  201.   char buf[BUFSIZ];
  202.   char response[BUFSIZ];
  203.   int s;
  204.   int len;
  205.  
  206.   if ((len = read(0,buf,BUFSIZ)) < 0) {
  207.     perror(progname);
  208.     fprintf(stderr,"%s: unable to read\n",progname);
  209.     exit(1);
  210.   }; /* if */
  211.       
  212.   /* parse the response from gnu */
  213.   response[0] = '\0';
  214.   sscanf(buf,"%d:%[^\n]\n", &s, response);
  215.  
  216.   /* Send a response back to the client. */
  217.   send_string(s,response);
  218.   close(s);
  219.  
  220. } /* handle_response */
  221. #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
  222.  
  223.  
  224. #ifdef INTERNET_DOMAIN_SOCKETS
  225. struct entry {
  226.   u_long host_addr;
  227.   struct entry *next;
  228. };
  229.  
  230. struct entry *permitted_hosts[TABLE_SIZE];
  231.  
  232.  
  233. /*
  234.   permitted -- return whether a given host is allowed to connect to the server.
  235. */
  236. int permitted(host_addr)
  237.      u_long host_addr;
  238. {
  239.   int key;
  240.   struct entry *entry;
  241.   
  242.   /* First find the hash key */
  243.   key = HASH(host_addr) % TABLE_SIZE;
  244.   
  245.   /* Now check the chain for that hash key */
  246.   for(entry=permitted_hosts[key]; entry != NULL; entry=entry->next)
  247.     if (host_addr == entry->host_addr) 
  248.       return(TRUE);
  249.  
  250.   return(FALSE);
  251.  
  252. } /* permitted */
  253.  
  254.  
  255. /* 
  256.   add_host -- add the given host to the list of permitted hosts, provided it isn't
  257.               already there.
  258. */    
  259. void add_host(host_addr)
  260.      u_long host_addr;
  261. {
  262.   int key;
  263.   struct entry *new_entry;
  264.   
  265.   if (!permitted(host_addr)) {
  266.     if ((new_entry = (struct entry *) malloc(sizeof(struct entry))) == NULL) {
  267.       fprintf(stderr,"%s: unable to malloc space for permitted host entry\n",
  268.           progname);
  269.       exit(1);
  270.     }; /* if */
  271.  
  272.     new_entry->host_addr = host_addr;
  273.     key = HASH(host_addr) % TABLE_SIZE;
  274.     new_entry->next = permitted_hosts[key];
  275.     permitted_hosts[key] = new_entry;
  276.   }; /* if */
  277.  
  278. } /* add_host */
  279.  
  280.  
  281. /*
  282.   setup_table -- initialise the table of hosts allowed to contact the server. 
  283.                  Put in the local machine, and, if a security file is specifed,
  284.                  add each host that is named in the file.
  285. */
  286. void setup_table()
  287. {
  288.   FILE *host_file;
  289.   char *file_name;
  290.   char hostname[HOSTNAMSZ];
  291.   u_long host_addr;
  292.   int i;
  293.   
  294.   /* Make sure every entry is null */
  295.   for (i=0; i<TABLE_SIZE; i++)
  296.     permitted_hosts[i] = NULL;
  297.  
  298.   gethostname(hostname,HOSTNAMSZ);
  299.  
  300.   if ((host_addr = internet_addr(hostname)) == -1) {
  301.     fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP", 
  302.         progname,hostname);
  303.     exit(1);
  304.   }; /* if */
  305.  
  306.   add_host(host_addr);                    /* add local host */
  307.  
  308.   if (((file_name = getenv("GNU_SECURE")) != NULL &&    /* security file  */
  309.        (host_file = fopen(file_name,"r")) != NULL)) {    /* opened ok */
  310.     while ((fscanf(host_file,"%s",hostname) != EOF))    /* find a host */
  311.       if ((host_addr = internet_addr(hostname)) != -1)    /* get its internet addr */
  312.     add_host(host_addr);                /* add the addr */
  313.  
  314.     fclose(host_file);
  315.   }; /* if */
  316.  
  317. } /* setup_table */
  318.  
  319.  
  320. /*
  321.   internet_init -- initialize server, returning an internet socket that can
  322.                     be listened on.
  323. */
  324. int internet_init()
  325. {
  326.   int ls;            /* socket descriptor */
  327.   struct servent *sp;        /* pointer to service information */
  328.   struct sockaddr_in server;    /* for local socket address */
  329.   char *ptr;            /* ptr to return from getenv */
  330.  
  331.   setup_table();
  332.  
  333.   /* clear out address structure */
  334.   bzero((char *)&server,sizeof(struct sockaddr_in));
  335.   
  336.   /* Set up address structure for the listen socket. */
  337.   server.sin_family = AF_INET;
  338.   server.sin_addr.s_addr = INADDR_ANY;
  339.  
  340.   /* Find the information for the gnu server
  341.    * in order to get the needed port number.
  342.    */
  343.   if ((ptr=getenv("GNU_PORT")) != NULL)
  344.     server.sin_port = htons(atoi(ptr));
  345.   else if ((sp = getservbyname ("gnuserv", "tcp")) == NULL)
  346.     server.sin_port = htons(DEFAULT_PORT+getuid());
  347.   else
  348.     server.sin_port = sp->s_port;
  349.   
  350.   /* Create the listen socket. */
  351.   if ((ls = socket (AF_INET,SOCK_STREAM, 0)) == -1) {
  352.     perror(progname);
  353.     fprintf(stderr,"%s: unable to create socket\n",progname);
  354.     exit(1);
  355.   }; /* if */
  356.   
  357.   /* Bind the listen address to the socket. */
  358.   if (bind(ls,&server,sizeof(struct sockaddr_in)) == -1) {
  359.     perror(progname);
  360.     fprintf(stderr,"%s: unable to bind socket\n",progname);
  361.     exit(1);
  362.   }; /* if */
  363.  
  364.   /* Initiate the listen on the socket so remote users
  365.    * can connect. 
  366.    */
  367.   if (listen(ls,20) == -1) {
  368.     perror(progname);
  369.     fprintf(stderr,"%s: unable to listen\n",progname);
  370.     exit(1);
  371.   }; /* if */
  372.  
  373.   return(ls);
  374.  
  375. } /* internet_init */
  376.  
  377.  
  378. /*
  379.   handle_internet_request -- accept a request from a client and send the information
  380.                              to stdout (the gnu process).
  381. */
  382. void handle_internet_request(ls)
  383. int ls;                /* listen socket */
  384. {
  385.   int s;
  386.   int addrlen = sizeof(struct sockaddr_in);
  387.   struct sockaddr_in peer;    /* for peer socket address */
  388.  
  389.   bzero((char *)&peer,sizeof(struct sockaddr_in));
  390.  
  391.   if ((s = accept(ls,&peer,&addrlen)) == -1) {
  392.     perror(progname);
  393.     fprintf(stderr,"%s: unable to accept\n",progname);
  394.     exit(1);
  395.   }; /* if */
  396.     
  397.   /* Check that access is allowed - if not return crud to the client */
  398.   if (!permitted(peer.sin_addr.s_addr)) {
  399.     send_string(s,"gnudoit: Connection refused\ngnudoit: unable to connect to remote");
  400.     close(s);
  401.     return;
  402.   }; /* if */
  403.  
  404.   echo_request(s);
  405.   
  406. } /* handle_internet_request */
  407. #endif /* INTERNET_DOMAIN_SOCKETS */
  408.  
  409.  
  410. #ifdef UNIX_DOMAIN_SOCKETS
  411. /*
  412.   unix_init -- initialize server, returning an unix-domain socket that can
  413.                be listened on.
  414. */
  415. int unix_init()
  416. {
  417.   int ls;            /* socket descriptor */
  418.   struct sockaddr_un server;     /* unix socket address */
  419.  
  420.   if ((ls = socket(AF_UNIX,SOCK_STREAM, 0)) < 0) {
  421.     perror(progname);
  422.     fprintf(stderr,"%s: unable to create socket\n",progname);
  423.     exit(1);
  424.   }; /* if */
  425.  
  426.   /* Set up address structure for the listen socket. */
  427.   sprintf(server.sun_path,"/tmp/gsrv%d",geteuid());
  428.   unlink(server.sun_path);    /* remove old file if it exists */
  429.  
  430.   server.sun_family = AF_UNIX;
  431.  
  432.   if (bind(ls,&server,strlen(server.sun_path)+2) < 0) {
  433.     perror(progname);
  434.     fprintf(stderr,"%s: unable to bind socket\n",progname);
  435.     exit(1);
  436.   }; /* if */
  437.  
  438.   chmod(server.sun_path,0700);    /* only this user can send commands */
  439.  
  440.   if (listen(ls,20) < 0) {
  441.     perror(progname);
  442.     fprintf(stderr,"%s: unable to listen\n",progname);
  443.     exit(1);
  444.   }; /* if */
  445.  
  446.   signal(SIGPIPE,SIG_IGN);    /* in case user kills client */
  447.  
  448.   return(ls);
  449.  
  450. } /* unix_init */
  451.  
  452.  
  453. /*
  454.   handle_unix_request -- accept a request from a client and send the information
  455.                          to stdout (the gnu process).
  456. */
  457. void handle_unix_request(ls)
  458. int ls;                /* listen socket */
  459. {
  460.   int s;
  461.   int len = sizeof(struct sockaddr_un);
  462.   struct sockaddr_un server;     /* for unix socket address */
  463.  
  464.   server.sun_family = AF_UNIX;
  465.  
  466.   if ((s = accept(ls,&server,&len)) < 0) {
  467.     perror(progname);
  468.     fprintf(stderr,"%s: unable to accept\n",progname);
  469.   }; /* if */
  470.  
  471.   echo_request(s);
  472.   
  473. } /* handle_unix_request */
  474. #endif /* UNIX_DOMAIN_SOCKETS */
  475.  
  476.  
  477. main(argc,argv)
  478.      int argc;
  479.      char *argv[];
  480. {
  481.   int ils = -1;            /* internet domain listen socket */
  482.   int uls = -1;            /* unix domain listen socket */
  483.   int chan;            /* temporary channel number */
  484. #ifdef SYSV_IPC
  485.   struct msgbuf *msgp;        /* message buffer */
  486. #endif /* SYSV_IPC */
  487.  
  488.   progname = argv[0];
  489.  
  490.   for(chan=3; chan < _NFILE; close(chan++)) /* close unwanted channels */
  491.     ;
  492.  
  493. #ifdef SYSV_IPC
  494.   ipc_init(&msgp);        /* get a msqid to listen on, and a message buffer */
  495. #endif /* SYSV_IPC */
  496.  
  497. #ifdef INTERNET_DOMAIN_SOCKETS
  498.   ils = internet_init();    /* get a internet domain socket to listen on */
  499. #endif /* INTERNET_DOMAIN_SOCKETS */
  500.  
  501. #ifdef UNIX_DOMAIN_SOCKETS
  502.   uls = unix_init();        /* get a unix domain socket to listen on */
  503. #endif /* UNIX_DOMAIN_SOCKETS */
  504.  
  505.   while (1) {
  506. #ifdef SYSV_IPC
  507.     handle_ipc_request(msgp);
  508. #else /* NOT SYSV_IPC */
  509.     int rmask = 1 
  510. #ifdef UNIX_DOMAIN_SOCKETS
  511.       + (1 << uls) 
  512. #endif /* UNIX_DOMAIN_SOCKETS */
  513.  
  514. #ifdef INTERNET_DOMAIN_SOCKETS
  515.       + (1 << ils)
  516. #endif /* INTERNET_DOMAIN_SOCKETS */
  517.       ;
  518.     
  519.     if (select(max2(uls,ils) + 1,&rmask,0,0,0) < 0) {
  520.       perror(progname);
  521.       fprintf(stderr,"%s: unable to select\n",progname);
  522.       exit(1);
  523.     }; /* if */
  524.  
  525. #ifdef UNIX_DOMAIN_SOCKETS    
  526.     if (rmask & (1 << uls))    /* from unix domain socket (client process) */
  527.       handle_unix_request(uls);
  528. #endif /* UNIX_DOMAIN_SOCKETS */
  529.  
  530. #ifdef INTERNET_DOMAIN_SOCKETS
  531.     if (rmask & (1 << ils))    /* from internet domain socket (client process) */
  532.       handle_internet_request(ils);
  533. #endif /* INTERNET_DOMAIN_SOCKETS */
  534.  
  535.     if (rmask & 1)         /* from stdin (gnu process) */
  536.       handle_response();
  537. #endif /* NOT SYSV_IPC */
  538.   }; /* while */
  539.  
  540. } /* main */
  541.  
  542. #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  543.