home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / LINUX_PC.TAR / pcnfsd_linux2 / pcnfsd_svc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-06  |  10.3 KB  |  464 lines

  1. /* RE_SID: @(%)/tmp_mnt/vol/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_svc.c 1.7 93/02/08 19:20:09 SMI */
  2. /*
  3.  * This file has been edited by hand.
  4.  * It was generated using rpcgen.
  5.  */
  6. /* The following was added to conform with SVR4 */
  7. #define PORTMAP
  8. /* End inclusions */
  9.  
  10. #include "common.h"
  11.  
  12. #include <stdio.h>
  13. #include <rpc/rpc.h>
  14. #include <signal.h>
  15. #include <sys/socket.h>
  16. #include <sys/ioctl.h>
  17.  
  18. /* The following changes for Ultrix 4.2 were suggested by various users */
  19. #ifndef ULTRIX
  20. #include <syslog.h>
  21. #else  /* ULTRIX */
  22. #include <sys/time.h>
  23. #include <sys/syslog.h>
  24. #endif /* ULTRIX */
  25. /* End of Ultrix changes */
  26.  
  27. /* The following was added to conform with SVR4 */
  28. #ifndef IPPROTO_IP
  29. #include <netinet/in.h>
  30. #endif
  31. #include <sys/resource.h>
  32. /* End inclusions */
  33. #include <netdb.h>
  34. #include "pcnfsd.h"
  35.  
  36. #ifdef DEBUG
  37. #define RPC_SVC_FG
  38. #endif
  39. #define _RPCSVC_CLOSEDOWN 120
  40. static _msgout();
  41. #if 1
  42.  void msg_out(msg) char *msg; {_msgout(msg);}
  43. #endif
  44. #if RPC_HDR
  45.  extern void msg_out();
  46. #endif
  47.  
  48. static void pcnfsdprog_1();
  49. static void pcnfsdprog_2();
  50. static void closedown();
  51.  
  52. static int _rpcpmstart;        /* Started by a port monitor ? */
  53. static int _rpcfdtype;        /* Whether Stream or Datagram ? */
  54. static int _rpcsvcdirty;    /* Still serving ? */
  55.  
  56. /*
  57. ** The following was added by hand:
  58. */
  59. SVCXPRT *caller;
  60. extern void config_from_file();
  61.  
  62. main()
  63. {
  64.     register SVCXPRT *transp;
  65.     int sock;
  66.     int proto;
  67.     struct sockaddr_in saddr;
  68.     int asize = sizeof(saddr);
  69.  
  70.     if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
  71.         int ssize = sizeof(int);
  72.  
  73.         if (saddr.sin_family != AF_INET)
  74.             exit(1);
  75.         if (getsockopt(0, SOL_SOCKET, SO_TYPE,
  76.                 (char *)&_rpcfdtype, &ssize) == -1)
  77.             exit(1);
  78.         sock = 0;
  79.         _rpcpmstart = 1;
  80.         proto = 0;
  81.         openlog("pcnfsd", LOG_PID, LOG_DAEMON);
  82.     } else {
  83. #ifndef RPC_SVC_FG
  84.         int i, pid;
  85.  
  86.         pid = fork();
  87.         if (pid < 0) {
  88.             perror("cannot fork");
  89.             exit(1);
  90.         }
  91.         if (pid)
  92.             exit(0);
  93.         for (i = 0 ; i < 20; i++)
  94.             (void) close(i);
  95. #ifdef TIOCNOTTY
  96.         i = open("/dev/console", 2);
  97.         (void) dup2(i, 1);
  98.         (void) dup2(i, 2);
  99.         i = open("/dev/tty", 2);
  100.         if (i >= 0) {
  101.             (void) ioctl(i, TIOCNOTTY, (char *)NULL);
  102.             (void) close(i);
  103.         }
  104. #else TIOCNOTTY
  105.         (void)setsid();
  106. #endif TIOCNOTTY
  107.         openlog("pcnfsd", LOG_PID, LOG_DAEMON);
  108. #endif RPC_SVC_FG
  109.         sock = RPC_ANYSOCK;
  110.         (void) pmap_unset(PCNFSDPROG, PCNFSDVERS);
  111.         (void) pmap_unset(PCNFSDPROG, PCNFSDV2);
  112.     }
  113.  
  114.     if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
  115.         transp = svcudp_create(sock);
  116.         if (transp == NULL) {
  117.             _msgout("cannot create udp service.");
  118.             exit(1);
  119.         }
  120.         if (!_rpcpmstart)
  121.             proto = IPPROTO_UDP;
  122.         if (!svc_register(transp, PCNFSDPROG, PCNFSDVERS, pcnfsdprog_1, proto)) {
  123.             _msgout("unable to register (PCNFSDPROG, PCNFSDVERS, udp).");
  124.             exit(1);
  125.         }
  126.         if (!svc_register(transp, PCNFSDPROG, PCNFSDV2, pcnfsdprog_2, proto)) {
  127.             _msgout("unable to register (PCNFSDPROG, PCNFSDV2, udp).");
  128.             exit(1);
  129.         }
  130.     }
  131.  
  132.     if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
  133.         transp = svctcp_create(sock, 0, 0);
  134.         if (transp == NULL) {
  135.             _msgout("cannot create tcp service.");
  136.             exit(1);
  137.         }
  138.         if (!_rpcpmstart)
  139.             proto = IPPROTO_TCP;
  140.         if (!svc_register(transp, PCNFSDPROG, PCNFSDVERS, pcnfsdprog_1, proto)) {
  141.             _msgout("unable to register (PCNFSDPROG, PCNFSDVERS, tcp).");
  142.             exit(1);
  143.         }
  144.         if (!svc_register(transp, PCNFSDPROG, PCNFSDV2, pcnfsdprog_2, proto)) {
  145.             _msgout("unable to register (PCNFSDPROG, PCNFSDV2, tcp).");
  146.             exit(1);
  147.         }
  148.     }
  149.  
  150.     if (transp == (SVCXPRT *)NULL) {
  151.         _msgout("could not create a handle");
  152.         exit(1);
  153.     }
  154.     if (_rpcpmstart) {
  155.         (void) signal(SIGALRM, closedown);
  156.         (void) alarm(_RPCSVC_CLOSEDOWN);
  157.     }
  158. /*
  159. ** The following was added by hand:
  160. */
  161.     config_from_file();
  162.  
  163. #ifdef RPC_SVC_FG
  164.     _msgout("rpc.pcnfsd version 2 ready to service requests");
  165. #endif RPC_SVC_FG
  166.  
  167.     svc_run();
  168.     _msgout("svc_run returned");
  169.     exit(1);
  170.     /* NOTREACHED */
  171. }
  172.  
  173. static void
  174. pcnfsdprog_1(rqstp, transp)
  175.     struct svc_req *rqstp;
  176.     register SVCXPRT *transp;
  177. {
  178.     union {
  179.         auth_args pcnfsd_auth_1_arg;
  180.         pr_init_args pcnfsd_pr_init_1_arg;
  181.         pr_start_args pcnfsd_pr_start_1_arg;
  182.     } argument;
  183.     char *result;
  184.     bool_t (*xdr_argument)(), (*xdr_result)();
  185.     char *(*local)();
  186.  
  187. /*
  188. ** The following was added by hand:
  189. */
  190.     caller = transp;
  191.  
  192.     _rpcsvcdirty = 1;
  193.     switch (rqstp->rq_proc) {
  194.     case PCNFSD_NULL:
  195.         xdr_argument = xdr_void;
  196.         xdr_result = xdr_void;
  197.         local = (char *(*)()) pcnfsd_null_1;
  198.         break;
  199.  
  200.     case PCNFSD_AUTH:
  201.         xdr_argument = xdr_auth_args;
  202.         xdr_result = xdr_auth_results;
  203.         local = (char *(*)()) pcnfsd_auth_1;
  204.         break;
  205.  
  206.     case PCNFSD_PR_INIT:
  207.         xdr_argument = xdr_pr_init_args;
  208.         xdr_result = xdr_pr_init_results;
  209.         local = (char *(*)()) pcnfsd_pr_init_1;
  210.         break;
  211.  
  212.     case PCNFSD_PR_START:
  213.         xdr_argument = xdr_pr_start_args;
  214.         xdr_result = xdr_pr_start_results;
  215.         local = (char *(*)()) pcnfsd_pr_start_1;
  216.         break;
  217.  
  218.     default:
  219.         svcerr_noproc(transp);
  220.         _rpcsvcdirty = 0;
  221.         return;
  222.     }
  223.     (void) memset((char *)&argument, 0, sizeof (argument));
  224.     if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
  225.         svcerr_decode(transp);
  226.         _rpcsvcdirty = 0;
  227.         return;
  228.     }
  229.     result = (*local)(&argument, rqstp);
  230.     if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
  231.         svcerr_systemerr(transp);
  232.     }
  233.     if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
  234.         _msgout("unable to free arguments");
  235.         exit(1);
  236.     }
  237.     _rpcsvcdirty = 0;
  238.     return;
  239. }
  240.  
  241. static void
  242. pcnfsdprog_2(rqstp, transp)
  243.     struct svc_req *rqstp;
  244.     register SVCXPRT *transp;
  245. {
  246.     union {
  247.         v2_info_args pcnfsd2_info_2_arg;
  248.         v2_pr_init_args pcnfsd2_pr_init_2_arg;
  249.         v2_pr_start_args pcnfsd2_pr_start_2_arg;
  250.         v2_pr_queue_args pcnfsd2_pr_queue_2_arg;
  251.         v2_pr_status_args pcnfsd2_pr_status_2_arg;
  252.         v2_pr_cancel_args pcnfsd2_pr_cancel_2_arg;
  253.         v2_pr_admin_args pcnfsd2_pr_admin_2_arg;
  254.         v2_pr_requeue_args pcnfsd2_pr_requeue_2_arg;
  255.         v2_pr_hold_args pcnfsd2_pr_hold_2_arg;
  256.         v2_pr_release_args pcnfsd2_pr_release_2_arg;
  257.         v2_mapid_args pcnfsd2_mapid_2_arg;
  258.         v2_auth_args pcnfsd2_auth_2_arg;
  259.         v2_alert_args pcnfsd2_alert_2_arg;
  260.     } argument;
  261.     char *result;
  262.     bool_t (*xdr_argument)(), (*xdr_result)();
  263.     char *(*local)();
  264.  
  265. /*
  266. ** The following was added by hand:
  267. */
  268.     caller = transp;
  269.  
  270.     _rpcsvcdirty = 1;
  271.     switch (rqstp->rq_proc) {
  272.     case PCNFSD2_NULL:
  273.         xdr_argument = xdr_void;
  274.         xdr_result = xdr_void;
  275.         local = (char *(*)()) pcnfsd2_null_2;
  276.         break;
  277.  
  278.     case PCNFSD2_INFO:
  279.         xdr_argument = xdr_v2_info_args;
  280.         xdr_result = xdr_v2_info_results;
  281.         local = (char *(*)()) pcnfsd2_info_2;
  282.         break;
  283.  
  284.     case PCNFSD2_PR_INIT:
  285.         xdr_argument = xdr_v2_pr_init_args;
  286.         xdr_result = xdr_v2_pr_init_results;
  287.         local = (char *(*)()) pcnfsd2_pr_init_2;
  288.         break;
  289.  
  290.     case PCNFSD2_PR_START:
  291.         xdr_argument = xdr_v2_pr_start_args;
  292.         xdr_result = xdr_v2_pr_start_results;
  293.         local = (char *(*)()) pcnfsd2_pr_start_2;
  294.         break;
  295.  
  296.     case PCNFSD2_PR_LIST:
  297.         xdr_argument = xdr_void;
  298.         xdr_result = xdr_v2_pr_list_results;
  299.         local = (char *(*)()) pcnfsd2_pr_list_2;
  300.         break;
  301.  
  302.     case PCNFSD2_PR_QUEUE:
  303.         xdr_argument = xdr_v2_pr_queue_args;
  304.         xdr_result = xdr_v2_pr_queue_results;
  305.         local = (char *(*)()) pcnfsd2_pr_queue_2;
  306.         break;
  307.  
  308.     case PCNFSD2_PR_STATUS:
  309.         xdr_argument = xdr_v2_pr_status_args;
  310.         xdr_result = xdr_v2_pr_status_results;
  311.         local = (char *(*)()) pcnfsd2_pr_status_2;
  312.         break;
  313.  
  314.     case PCNFSD2_PR_CANCEL:
  315.         xdr_argument = xdr_v2_pr_cancel_args;
  316.         xdr_result = xdr_v2_pr_cancel_results;
  317.         local = (char *(*)()) pcnfsd2_pr_cancel_2;
  318.         break;
  319.  
  320.     case PCNFSD2_PR_ADMIN:
  321.         xdr_argument = xdr_v2_pr_admin_args;
  322.         xdr_result = xdr_v2_pr_admin_results;
  323.         local = (char *(*)()) pcnfsd2_pr_admin_2;
  324.         break;
  325.  
  326.     case PCNFSD2_PR_REQUEUE:
  327.         xdr_argument = xdr_v2_pr_requeue_args;
  328.         xdr_result = xdr_v2_pr_requeue_results;
  329.         local = (char *(*)()) pcnfsd2_pr_requeue_2;
  330.         break;
  331.  
  332.     case PCNFSD2_PR_HOLD:
  333.         xdr_argument = xdr_v2_pr_hold_args;
  334.         xdr_result = xdr_v2_pr_hold_results;
  335.         local = (char *(*)()) pcnfsd2_pr_hold_2;
  336.         break;
  337.  
  338.     case PCNFSD2_PR_RELEASE:
  339.         xdr_argument = xdr_v2_pr_release_args;
  340.         xdr_result = xdr_v2_pr_release_results;
  341.         local = (char *(*)()) pcnfsd2_pr_release_2;
  342.         break;
  343.  
  344.     case PCNFSD2_MAPID:
  345.         xdr_argument = xdr_v2_mapid_args;
  346.         xdr_result = xdr_v2_mapid_results;
  347.         local = (char *(*)()) pcnfsd2_mapid_2;
  348.         break;
  349.  
  350.     case PCNFSD2_AUTH:
  351.         xdr_argument = xdr_v2_auth_args;
  352.         xdr_result = xdr_v2_auth_results;
  353.         local = (char *(*)()) pcnfsd2_auth_2;
  354.         break;
  355.  
  356.     case PCNFSD2_ALERT:
  357.         xdr_argument = xdr_v2_alert_args;
  358.         xdr_result = xdr_v2_alert_results;
  359.         local = (char *(*)()) pcnfsd2_alert_2;
  360.         break;
  361.  
  362.     default:
  363.         svcerr_noproc(transp);
  364.         _rpcsvcdirty = 0;
  365.         return;
  366.     }
  367.     (void) memset((char *)&argument, 0, sizeof (argument));
  368.     if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
  369.         svcerr_decode(transp);
  370.         _rpcsvcdirty = 0;
  371.         return;
  372.     }
  373.     result = (*local)(&argument, rqstp);
  374.     if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
  375.         svcerr_systemerr(transp);
  376.     }
  377.     if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
  378.         _msgout("unable to free arguments");
  379.         exit(1);
  380.     }
  381.     _rpcsvcdirty = 0;
  382.     return;
  383. }
  384.  
  385. static
  386. _msgout(msg)
  387.     char *msg;
  388. {
  389. #ifdef RPC_SVC_FG
  390.     if (_rpcpmstart)
  391.         syslog(LOG_ERR, msg);
  392.     else
  393.         (void) fprintf(stderr, "%s\n", msg);
  394. #else
  395.     syslog(LOG_ERR, msg);
  396. #endif
  397.     return(0);
  398. }
  399.  
  400. static void
  401. closedown()
  402. {
  403.     if (_rpcsvcdirty == 0) {
  404.         extern fd_set svc_fdset;
  405.         static int size;
  406.         int i, openfd;
  407.  
  408.         if (_rpcfdtype == SOCK_DGRAM)
  409.             exit(0);
  410. #ifdef RLIMIT_NOFILE
  411.          if (size == 0) {
  412.              struct rlimit rl;
  413.  
  414.              rl.rlim_max = 0;
  415.              getrlimit(RLIMIT_NOFILE, &rl);
  416.              if ((size = rl.rlim_max) == 0)
  417.                  return;
  418.          }
  419. #else RLIMIT_NOFILE
  420. /* XXX could use getdtablesize() here, but I don't know who uses it */
  421.         size = 20;
  422. #endif RLIMIT_NOFILE
  423.         for (i = 0, openfd = 0; i < size && openfd < 2; i++)
  424.             if (FD_ISSET(i, &svc_fdset))
  425.                 openfd++;
  426.         if (openfd <= 1)
  427.             exit(0);
  428.     }
  429.     (void) alarm(_RPCSVC_CLOSEDOWN);
  430. }
  431.  
  432. /*
  433. ** The following was added by hand.
  434. */
  435.  
  436. char *
  437. getcallername()
  438. {
  439.         struct sockaddr_in actual;
  440.         struct hostent *hp;
  441.         static struct in_addr prev;
  442.     static char cname[128];
  443.  
  444.         actual = *svc_getcaller(caller);
  445.  
  446.         if (memcmp((char *)&actual.sin_addr, (char *)&prev,
  447.          sizeof(struct in_addr)) == 0)
  448.                 return (cname);
  449.  
  450.         prev = actual.sin_addr;
  451.  
  452.         hp = gethostbyaddr((char *) &actual.sin_addr, sizeof(actual.sin_addr), 
  453.                            AF_INET);
  454.         if (hp == NULL) {                       /* dummy one up */
  455.         extern char *inet_ntoa();
  456.                 strcpy(cname, inet_ntoa(actual.sin_addr));
  457.          } else {
  458.                 strcpy(cname, hp->h_name);
  459.         }
  460.  
  461.         return (cname);
  462. }
  463.  
  464.