1 /* $TOG: svcmain.c /main/10 1998/04/06 13:13:49 mgreess $ */
3 * (c) Copyright 1993, 1994 Hewlett-Packard Company
4 * (c) Copyright 1993, 1994 International Business Machines Corp.
5 * (c) Copyright 1993, 1994 Novell, Inc.
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
16 #include <sys/resource.h>
19 #include <netinet/in.h>
20 #endif /* _NETINET_IN_H */
23 #if defined(SunOS) || defined(USL) || defined(__uxp__)
24 #include <netconfig.h>
26 #include <sys/stropts.h>
28 #endif /* SunOS || USL || __uxp__ */
29 #include <sys/param.h>
34 #include <sys/signal.h>
37 #include "rpcextras.h"
39 #include "cmscalendar.h"
45 #define S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
48 #define S_IRWXG (S_IRGRP|S_IWGRP|S_IXGRP)
51 #define S_IRWXO (S_IROTH|S_IWOTH|S_IXOTH)
54 #define S_MASK (S_INPUT|S_HIPRI|S_ERROR|S_HANGUP)
57 static int standalone; /* default is 0 */
58 static int received_sighup = 0; /* 1 means we get SIGHUP */
59 static int rpc_in_process = 0; /* 1 means processing client request */
60 static int garbage_collection_time = 240; /* in min; default time is 4:00am */
68 * get garbage collection time
69 * the given string should be in the format hhmm
70 * where hh is 0 - 23 and mm is 00 - 59
73 _GetGtime(char *timestr)
75 int hour, minute, len, i;
80 if ((len = strlen(timestr)) > 4)
83 for (i = 0; i < len; i++) {
84 if (timestr[i] < '0' || timestr[i] > '9')
88 minute = atoi(×tr[len - 2]);
89 timestr[len - 2] = NULL;
92 if (hour > 23 || minute > 59)
95 garbage_collection_time = hour * 60 + minute;
100 fprintf(stderr, "The time specified is invalid.\n");
105 parse_args(int argc, char **argv)
109 if (pgname = strrchr (argv[0], '/'))
114 while ((opt = getopt (argc, argv, "dsg:")) != -1)
125 if (_GetGtime(optarg))
138 * rpc.cmsd gets started by the inetd.
139 * On AIX inetd requires that two arguments be supplied to the RPC
140 * programs as follows (from the inetd.conf man page):
142 * ServerArgs Specifies the command line arguments that the
143 * inetd daemon should use to execute the server. The maximum number
144 * of arguments is five. The first argument specifies the name of the
145 * server used. If the SocketType parameter is sunrpc_tcp or
146 * sunrpc_udp, * the second argument specifies the program name and
147 * the third argument specifies the version of the program. For
148 * services that the inetd daemon provides internally, this field
152 else if (optind == 1 && argc >= 3)
160 argv_r = (char **) malloc(argc * sizeof(char *));
163 for (i=optind+2, j=1; i<argc; i++,j++)
165 parse_args(argc-2, argv_r);
167 free((void *) argv_r);
173 fprintf (stderr, "Usage: %s [-d] [-s] [-g hhmm]\n", pgname);
180 char *dir = _DtCMS_DEFAULT_DIR;
189 "%s: must be run in super-user mode! Exited.\n",
195 if (stat(dir, &info))
197 /* if directory does not exist, create the directory */
198 if ((errno != ENOENT) || mkdir(dir, S_IRWXU|S_IRWXG|S_IRWXO))
201 sprintf(msgbuf, "%s: cannot create %s.\n%s: %s",
202 pgname, dir, pgname, "System error");
204 sprintf(msgbuf, "%s: cannot access %s.\n%s: %s",
205 pgname, dir, pgname, "System error");
212 /* if dir is just created, we need to do chmod and chown.
213 * Otherwise, only do chmod and/or chown if permssion and/or
214 * ownership is wrong.
216 mode = S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO;
218 if (create_dir || info.st_mode != (mode | S_IFDIR)) {
220 /* set directory permission to be "rwxrwsrwt" */
221 if (chmod(dir, mode)) {
222 sprintf(msgbuf, "%s: Permission on %s%s\n%s%s\n%s%s",
224 " is wrong but cannot be corrected.", pgname,
225 ": This might happen if you are mounting the directory.",
226 pgname, ": System error");
234 if (create_dir || info.st_uid!=daemon_uid || info.st_gid!=daemon_gid) {
235 /* set directory ownership to: owner = 1, group = 1 */
236 if (chown(dir, daemon_uid, daemon_gid)) {
237 sprintf(msgbuf, "%s: Ownership on %s%s\n%s%s\n%s%s",
239 " is wrong but cannot be corrected.", pgname,
240 ": This might happen if you are mounting the directory.",
241 pgname, ": System error");
249 /* Change current directory, so core file can be dumped. */
254 * send a SIGHUP signal to the rpc.cmsd that is already running
261 pid_t pid, mypid = getpid();
262 extern FILE *popen(const char *, const char *);
263 extern int pclose(FILE *);
265 sprintf(buf, "ps -e|grep rpc.cmsd|grep -v grep");
267 if ((fp = popen(buf, "r")) == NULL) {
269 fprintf(stderr, "rpc.cmsd: popen failed\n");
271 while (fgets(buf, sizeof(buf), fp) != NULL) {
272 if ((pid = atol(buf)) != mypid) {
273 if (kill(pid, SIGHUP))
274 perror("rpc.cmsd: failed to send SIGHUP");
276 fprintf(stderr, "rpc.cmsd: %s %ld\n",
277 "sent SIGHUP to", (long)pid);
285 * We only allow one rpc.cmsd to run on each machine.
290 char *dir = _DtCMS_DEFAULT_DIR;
291 char buff [MAXPATHLEN];
296 locker.l_type = F_WRLCK;
303 strcat (buff, "/.lock.");
306 * /var/spool might be mounted. Use .lock.hostname to
307 * prevent more than one cms running in each host.
309 strcat(buff, _DtCmGetLocalHost());
311 fd = open(buff, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
319 * Note, we have to use flock() instead of lockf() because cms process
320 * is run in each host.
323 if (fcntl (fd, F_SETLK, &locker) != 0)
325 if (flock (fd, LOCK_EX|LOCK_NB) != 0)
332 if (error != EWOULDBLOCK && error != EACCES) {
334 perror ("rpc.cmsd: failed to lock lockfile");
335 fprintf(stderr, "error = %d\n", error);
340 fprintf(stderr, "rpc.cmsd: %s\n",
341 "lock_it failed due to another process");
343 /* cms has been running.... */
352 program(struct svc_req *rqstp, register SVCXPRT *transp)
355 char *argument = NULL;
356 program_handle ph = getph();
357 struct rpcgen_table *proc;
359 /* set rpc_in_process so that sighup handler won't exit right away */
362 /* first do some bounds checking: */
363 if (rqstp->rq_vers >= ph->nvers) {
364 svcerr_noproc(transp);
367 if (ph->prog[rqstp->rq_vers].nproc == 0) {
368 svcerr_noproc(transp);
371 if (rqstp->rq_proc >= ph->prog[rqstp->rq_vers].nproc) {
372 svcerr_noproc(transp);
376 if (rqstp->rq_proc == NULLPROC) {
377 if (debug) fprintf(stderr, "rpc.cmsd: ping\n");
378 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL);
382 /* assert - the program number, version and proc numbers are valid */
383 proc = &(ph->prog[rqstp->rq_vers].vers[rqstp->rq_proc]);
384 argument = (char*)calloc(proc->len_arg, sizeof(char));
385 if (!svc_getargs(transp, proc->xdr_arg, argument)) {
386 svcerr_decode(transp);
390 result = (*proc->proc)(argument, rqstp);
391 if (result != NULL && !svc_sendreply(transp, proc->xdr_res, result)) {
392 svcerr_systemerr(transp);
395 if (!svc_freeargs(transp, proc->xdr_arg, argument)) {
396 (void)fprintf(stderr, "unable to free arguments");
404 /* exit if we have received the SIGHUP signal */
405 if (received_sighup == 1) {
407 fprintf(stderr, "rpc.cmsd: received SIGHUP, %s",
408 "exiting after finished processing\n");
414 * Signal handler for SIGHUP.
415 * If we are in the middle of processing a client request,
416 * finish processing before we exit.
419 sighup_handler(int sig_num)
422 fprintf(stderr, "rpc.cmsd: sighup received\n");
424 if (rpc_in_process == 0) {
426 fprintf(stderr, "rpc.cmsd: exit from sighup_handler\n");
430 fprintf(stderr, "rpc.cmsd: set received_sighup to 1\n");
436 * garbage_collection_time (in min) is the time to do garbage collection
438 * This routine returns the difference between the first garbage collection
439 * time and now so that the calling routine can set the alarm.
442 _GetFirstGarbageCollectionTime()
444 int n=0, midnight=0, gtime=0;
448 /* try today first */
449 midnight = next_ndays(n, 0);
450 gtime = next_nmins(midnight, garbage_collection_time);
453 /* the first garbage collection will be done tomorrow */
454 midnight = next_ndays(n, 1);
455 gtime = next_nmins(midnight, garbage_collection_time);
465 extern void garbage_collect();
466 extern void debug_switch();
468 #if defined(SVR4) && !defined(linux)
469 extern void (*sigset(int, void (*)(int)))(int);
470 sigset(SIGUSR1, garbage_collect);
471 sigset(SIGALRM, garbage_collect);
472 sigset(SIGUSR2, debug_switch);
474 signal(SIGUSR1, garbage_collect);
475 signal(SIGALRM, garbage_collect);
476 signal(SIGUSR2, debug_switch);
479 next = _GetFirstGarbageCollectionTime();
480 alarm((unsigned) next);
483 main(int argc, char **argv)
486 program_handle ph = newph();
490 struct sockaddr_in saddr;
491 int asize = sizeof (saddr);
492 SVCXPRT *tcp_transp = (SVCXPRT *)-1;
493 SVCXPRT *udp_transp = (SVCXPRT *)-1;
496 #if defined(SunOS) || defined(USL) || defined(__uxp__)
497 struct netconfig *nconf_udp;
498 struct netconfig *nconf_tcp;
500 #if !defined(USL) || (defined(USL) && (OSMAJORVERSION > 1))
501 char mname[FMNAMESZ+1];
503 #endif /* SunOS || USL */
505 pw = (struct passwd *)getpwnam("daemon");
506 gr = (struct group *)getgrnam("daemon");
508 daemon_uid = pw->pw_uid;
512 daemon_gid = gr->gr_gid;
516 parse_args(argc, argv);
518 /* check to see if we are started by inetd */
519 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
523 #if defined(SunOS) || defined(USL) || defined(__uxp__)
524 #if !defined(USL) || (defined(USL) && (OSMAJORVERSION > 1))
525 /* we need a TLI endpoint rather than a socket */
526 if (ioctl(0, I_LOOK, mname) != 0) {
527 perror("rpc.cmsd: ioctl failed to get module name");
530 if (strcmp(mname, "sockmod") == 0) {
531 /* Change socket fd to TLI fd */
532 if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
533 perror("rpc.cmsd: ioctl I_POP/I_PUSH failed");
536 } else if (strcmp(mname, "timod") != 0) {
537 fprintf(stderr, "rpc.cmsd: fd 0 is not timod\n");
540 #else /* !USL || (USL && OSMAJORVERSION > 1) */
541 if (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, "timod")) {
542 perror("rpc.cmsd: ioctl I_POP/I_PUSH failed");
545 #endif /* !USL || (USL && OSMAJORVERSION > 1) */
547 } else if (t_getinfo(0, &info) == 0) {
550 #endif /* SunOS || USL */
556 * if it is started by inetd, make stderr to be
560 if ((fd = open ("/dev/console", O_WRONLY)) >= 0) {
568 /* Set up private directory and switch euid/egid to daemon. */
572 /* Don't allow multiple cms processes running in the same host. */
573 if ((error = lock_it()) != 0 && !standalone) {
574 /* we are invoked by inetd but another rpc.cmsd
575 * is alreay running, so send SIGHUP to it
580 /* try to lock it again */
581 if (lock_it() != 0) {
583 fprintf(stderr, "cm: rpc.cmsd is still running\n");
587 } else if (error != 0) {
588 fprintf(stderr, "rpc.cmsd: rpc.cmsd is already running.\n");
592 /* use signal because we only need it once */
593 signal(SIGHUP, sighup_handler);
596 #if defined(SunOS) || defined(USL) || defined(__uxp__)
597 /* raise the soft limit of number of file descriptor */
598 /* this is to prevent the backend from running out of open file des */
599 getrlimit(RLIMIT_NOFILE, &rl);
600 rl.rlim_cur = (rl.rlim_max <= 256) ? rl.rlim_max : 256;
601 setrlimit(RLIMIT_NOFILE, &rl);
604 #if defined(SunOS) || defined(USL) || defined(__uxp__)
605 nconf_udp = getnetconfigent("udp");
606 nconf_tcp = getnetconfigent("tcp");
608 for (version = 0; version < ph->nvers; version++) {
609 /* don't register unsupported versions: */
610 if (ph->prog[version].nproc == 0) continue;
613 rpcb_unset(ph->program_num, version, NULL);
616 "rpc.cmsd: rpcb_unset for version %ld\n",
620 /* brought up by inetd, use fd 0 which must be a TLI fd */
621 if (udp_transp == (SVCXPRT *)-1) {
622 udp_transp = svc_tli_create(standalone ? RPC_ANYFD : 0,
623 nconf_udp, (struct t_bind*) NULL, 0, 0);
625 if (udp_transp == NULL) {
626 t_error("rtable_main.c: svc_tli_create(udp)");
631 if (svc_reg(udp_transp, ph->program_num, version, program,
632 standalone ? nconf_udp : NULL) == 0) {
633 t_error("rtable_main.c: svc_reg");
637 /* Set up tcp for calls that potentially return */
638 /* large amount of data. This transport is not */
639 /* registered with inetd so need to register it */
640 /* with rpcbind ourselves. */
642 rpcb_unset(ph->program_num, version, nconf_tcp);
644 if (tcp_transp == (SVCXPRT *)-1) {
645 tcp_transp = svc_tli_create(RPC_ANYFD, nconf_tcp,
646 (struct t_bind *)NULL, 0, 0);
648 if (tcp_transp == NULL) {
649 t_error("rtable_main.c: svc_til_create(tcp)");
654 if (svc_reg(tcp_transp, ph->program_num, version, program,
656 t_error("rtable_main.c: svc_reg(tcp)");
662 freenetconfigent(nconf_udp);
664 freenetconfigent(nconf_tcp);
668 for (version = 0; version < ph->nvers; version++) {
669 /* don't register unsupported versions: */
670 if (ph->prog[version].nproc == 0) continue;
675 (void) pmap_unset(ph->program_num, version);
677 if (udp_transp == (SVCXPRT *)-1) {
678 udp_transp = svcudp_create(standalone ? RPC_ANYSOCK : 0
679 #if defined(_AIX) || defined(hpV4) || defined(__osf__) || defined(linux)
684 if (udp_transp == NULL) {
685 (void)fprintf(stderr,
686 "rtable_main.c: cannot create udp service.\n");
692 if (!svc_register(udp_transp, ph->program_num, version, program,
693 standalone ? IPPROTO_UDP : 0)) {
695 if (!svc_register(udp_transp, ph->program_num, version, program,
698 (void)fprintf(stderr, "rtable_main.c: unable to register");
702 /* Set up tcp for calls that potentially return */
703 /* large amount of data. This transport is not */
704 /* registered with inetd so need to register it */
705 /* with rpcbind ourselves. */
707 if (tcp_transp == (SVCXPRT *)-1) {
708 tcp_transp = svctcp_create(RPC_ANYSOCK, 0, 0);
709 if (tcp_transp == NULL) {
710 (void)fprintf(stderr,
711 "rtable_main.c: cannot create tcp service.\n");
716 if (!svc_register(tcp_transp, ph->program_num, version, program,
718 (void)fprintf(stderr, "rtable_main.c: unable to register(tcp)");
723 #endif /* SunOS || USL */
730 setegid (daemon_gid);
731 seteuid (daemon_uid);
741 (void)fprintf(stderr, "rpc.cmsd: svc_run returned\n");