xref: /titanic_44/usr/src/cmd/fs.d/nfs/statd/sm_svc.c (revision 0523b0a4ec1f5ec136a264130012c7bb0ede01f2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <ftw.h>
45 #include <signal.h>
46 #include <string.h>
47 #include <syslog.h>
48 #include <netconfig.h>
49 #include <unistd.h>
50 #include <netdb.h>
51 #include <rpc/rpc.h>
52 #include <netinet/in.h>
53 #include <sys/param.h>
54 #include <sys/resource.h>
55 #include <sys/file.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/sockio.h>
59 #include <dirent.h>
60 #include <errno.h>
61 #include <rpcsvc/sm_inter.h>
62 #include <rpcsvc/nsm_addr.h>
63 #include <thread.h>
64 #include <synch.h>
65 #include <net/if.h>
66 #include <limits.h>
67 #include <rpcsvc/daemon_utils.h>
68 #include <priv_utils.h>
69 #include "sm_statd.h"
70 
71 
72 #define	home0		"/var/statmon"
73 #define	current0	"/var/statmon/sm"
74 #define	backup0		"/var/statmon/sm.bak"
75 #define	state0		"/var/statmon/state"
76 
77 #define	home1		"statmon"
78 #define	current1	"statmon/sm/"
79 #define	backup1		"statmon/sm.bak/"
80 #define	state1		"statmon/state"
81 
82 /*
83  * User and group IDs to run as.  These are hardwired, rather than looked
84  * up at runtime, because they are very unlikely to change and because they
85  * provide some protection against bogus changes to the passwd and group
86  * files.
87  */
88 
89 char STATE[MAXPATHLEN], CURRENT[MAXPATHLEN], BACKUP[MAXPATHLEN];
90 static char statd_home[MAXPATHLEN];
91 
92 int debug;
93 int regfiles_only = 0;		/* 1 => use symlinks in statmon, 0 => don't */
94 char hostname[MAXHOSTNAMELEN];
95 
96 /*
97  * These variables will be used to store all the
98  * alias names for the host, as well as the -a
99  * command line hostnames.
100  */
101 int host_name_count;
102 char **host_name; /* store -a opts */
103 int  addrix; /* # of -a entries */
104 
105 
106 /*
107  * The following 2 variables are meaningful
108  * only under a HA configuration.
109  * The path_name array is dynamically allocated in main() during
110  * command line argument processing for the -p options.
111  */
112 char **path_name = NULL;  /* store -p opts */
113 int  pathix = 0;  /* # of -p entries */
114 
115 /* Global variables.  Refer to sm_statd.h for description */
116 mutex_t crash_lock;
117 int die;
118 int in_crash;
119 cond_t crash_finish;
120 mutex_t sm_trylock;
121 rwlock_t thr_rwlock;
122 cond_t retrywait;
123 mutex_t name_addrlock;
124 
125 /* forward references */
126 static void set_statmon_owner(void);
127 static void copy_client_names(void);
128 static void one_statmon_owner(const char *);
129 static int nftw_owner(const char *, const struct stat *, int, struct FTW *);
130 
131 /*
132  * statd protocol
133  * 	commands:
134  * 		SM_STAT
135  * 			returns stat_fail to caller
136  * 		SM_MON
137  * 			adds an entry to the monitor_q and the record_q
138  *			This message is sent by the server lockd to the server
139  *			statd, to indicate that a new client is to be monitored.
140  *			It is also sent by the server lockd to the client statd
141  *			to indicate that a new server is to be monitored.
142  * 		SM_UNMON
143  * 			removes an entry from the monitor_q and the record_q
144  * 		SM_UNMON_ALL
145  * 			removes all entries from a particular host from the
146  * 			monitor_q and the record_q.  Our statd has this
147  * 			disabled.
148  * 		SM_SIMU_CRASH
149  * 			simulate a crash.  removes everything from the
150  * 			record_q and the recovery_q, then calls statd_init()
151  * 			to restart things.  This message is sent by the server
152  *			lockd to the server statd to have all clients notified
153  *			that they should reclaim locks.
154  * 		SM_NOTIFY
155  *			Sent by statd on server to statd on client during
156  *			crash recovery.  The client statd passes the info
157  *			to its lockd so it can attempt to reclaim the locks
158  *			held on the server.
159  *
160  * There are three main hash tables used to keep track of things.
161  * 	mon_table
162  * 		table that keeps track hosts statd must watch.  If one of
163  * 		these hosts crashes, then any locks held by that host must
164  * 		be released.
165  * 	record_table
166  * 		used to keep track of all the hostname files stored in
167  * 		the directory /var/statmon/sm.  These are client hosts who
168  *		are holding or have held a lock at some point.  Needed
169  *		to determine if a file needs to be created for host in
170  *		/var/statmon/sm.
171  *	recov_q
172  *		used to keep track hostnames during a recovery
173  *
174  * The entries are hashed based upon the name.
175  *
176  * There is a directory /var/statmon/sm which holds a file named
177  * for each host that is holding (or has held) a lock.  This is
178  * used during initialization on startup, or after a simulated
179  * crash.
180  */
181 
182 static void
183 sm_prog_1(rqstp, transp)
184 	struct svc_req *rqstp;
185 	SVCXPRT *transp;
186 {
187 	union {
188 		struct sm_name sm_stat_1_arg;
189 		struct mon sm_mon_1_arg;
190 		struct mon_id sm_unmon_1_arg;
191 		struct my_id sm_unmon_all_1_arg;
192 		struct stat_chge ntf_arg;
193 		struct reg1args reg1_arg;
194 	} argument;
195 
196 	union {
197 		sm_stat_res stat_resp;
198 		sm_stat	mon_resp;
199 		struct reg1res reg1_resp;
200 	} result;
201 
202 	bool_t (*xdr_argument)(), (*xdr_result)();
203 	char *(*local)();
204 
205 	/*
206 	 * Dispatch according to which protocol is being used:
207 	 *	NSM_ADDR_PROGRAM is the private lockd address
208 	 *		registration protocol.
209 	 *	SM_PROG is the normal statd (NSM) protocol.
210 	 */
211 	if (rqstp->rq_prog == NSM_ADDR_PROGRAM) {
212 		switch (rqstp->rq_proc) {
213 		case NULLPROC:
214 			svc_sendreply(transp, xdr_void, (caddr_t)NULL);
215 			return;
216 
217 		case NSMADDRPROC1_REG:
218 			xdr_argument = xdr_reg1args;
219 			xdr_result = xdr_reg1res;
220 			local = (char *(*)()) nsmaddrproc1_reg;
221 			break;
222 
223 		default:
224 			svcerr_noproc(transp);
225 			return;
226 		}
227 	} else {
228 		switch (rqstp->rq_proc) {
229 		case NULLPROC:
230 			svc_sendreply(transp, xdr_void, (caddr_t)NULL);
231 			return;
232 
233 		case SM_STAT:
234 			xdr_argument = xdr_sm_name;
235 			xdr_result = xdr_sm_stat_res;
236 			local = (char *(*)()) sm_status;
237 			break;
238 
239 		case SM_MON:
240 			xdr_argument = xdr_mon;
241 			xdr_result = xdr_sm_stat_res;
242 			local = (char *(*)()) sm_mon;
243 			break;
244 
245 		case SM_UNMON:
246 			xdr_argument = xdr_mon_id;
247 			xdr_result = xdr_sm_stat;
248 			local = (char *(*)()) sm_unmon;
249 			break;
250 
251 		case SM_UNMON_ALL:
252 			xdr_argument = xdr_my_id;
253 			xdr_result = xdr_sm_stat;
254 			local = (char *(*)()) sm_unmon_all;
255 			break;
256 
257 		case SM_SIMU_CRASH:
258 			xdr_argument = xdr_void;
259 			xdr_result = xdr_void;
260 			local = (char *(*)()) sm_simu_crash;
261 			break;
262 
263 		case SM_NOTIFY:
264 			xdr_argument = xdr_stat_chge;
265 			xdr_result = xdr_void;
266 			local = (char *(*)()) sm_notify;
267 			break;
268 
269 		default:
270 			svcerr_noproc(transp);
271 			return;
272 		}
273 	}
274 
275 	(void) memset(&argument, 0, sizeof (argument));
276 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
277 		svcerr_decode(transp);
278 		return;
279 	}
280 
281 	(void) memset(&result, 0, sizeof (result));
282 	(*local)(&argument, &result);
283 	if (!svc_sendreply(transp, xdr_result, (caddr_t)&result)) {
284 		svcerr_systemerr(transp);
285 	}
286 
287 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
288 			syslog(LOG_ERR, "statd: unable to free arguments\n");
289 		}
290 }
291 
292 /*
293  * Remove all files under directory path_dir.
294  */
295 static int
296 remove_dir(path_dir)
297 char *path_dir;
298 {
299 	DIR	*dp;
300 	struct dirent   *dirp;
301 	char tmp_path[MAXPATHLEN];
302 
303 	if ((dp = opendir(path_dir)) == (DIR *)NULL) {
304 		if (debug)
305 		    syslog(LOG_ERR,
306 			"warning: open directory %s failed: %m\n", path_dir);
307 		return (1);
308 	}
309 
310 	while ((dirp = readdir(dp)) != NULL) {
311 		if (strcmp(dirp->d_name, ".") != 0 &&
312 			strcmp(dirp->d_name, "..") != 0) {
313 			if (strlen(path_dir) + strlen(dirp->d_name) +2 >
314 				MAXPATHLEN) {
315 
316 				syslog(LOG_ERR,
317 		"statd: remove dir %s/%s failed.  Pathname too long.\n",
318 				path_dir, dirp->d_name);
319 
320 				continue;
321 			}
322 			(void) strcpy(tmp_path, path_dir);
323 			(void) strcat(tmp_path, "/");
324 			(void) strcat(tmp_path, dirp->d_name);
325 			delete_file(tmp_path);
326 		}
327 	}
328 
329 	(void) closedir(dp);
330 	return (0);
331 }
332 
333 /*
334  * Copy all files from directory `from_dir' to directory `to_dir'.
335  * Symlinks, if any, are preserved.
336  */
337 void
338 copydir_from_to(from_dir, to_dir)
339 char *from_dir;
340 char *to_dir;
341 {
342 	int	n;
343 	DIR	*dp;
344 	struct dirent   *dirp;
345 	char rname[MAXNAMELEN + 1];
346 	char path[MAXPATHLEN+MAXNAMELEN+2];
347 
348 	if ((dp = opendir(from_dir)) == (DIR *)NULL) {
349 		if (debug)
350 		    syslog(LOG_ERR,
351 			"warning: open directory %s failed: %m\n", from_dir);
352 		return;
353 	}
354 
355 	while ((dirp = readdir(dp)) != NULL) {
356 		if (strcmp(dirp->d_name, ".") == 0 ||
357 			strcmp(dirp->d_name, "..") == 0) {
358 			continue;
359 		}
360 
361 		(void) strcpy(path, from_dir);
362 		(void) strcat(path, "/");
363 		(void) strcat(path, dirp->d_name);
364 
365 		if (is_symlink(path)) {
366 			/*
367 			 * Follow the link to get the referenced file name
368 			 * and make a new link for that file in to_dir.
369 			 */
370 			n = readlink(path, rname, MAXNAMELEN);
371 			if (n <= 0) {
372 				if (debug >= 2) {
373 				    (void) printf(
374 					"copydir_from_to: can't read link %s\n",
375 					path);
376 				}
377 				continue;
378 			}
379 			rname[n] = '\0';
380 
381 			(void) create_symlink(to_dir, rname, dirp->d_name);
382 		} else {
383 			/*
384 			 * Simply copy regular files to to_dir.
385 			 */
386 			(void) strcpy(path, to_dir);
387 			(void) strcat(path, "/");
388 			(void) strcat(path, dirp->d_name);
389 			(void) create_file(path);
390 		}
391 	}
392 
393 	(void) closedir(dp);
394 }
395 
396 static int
397 init_hostname(void)
398 {
399 	struct lifnum lifn;
400 	int sock;
401 
402 	if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
403 		syslog(LOG_ERR, "statd:init_hostname, socket: %m");
404 		return (-1);
405 	}
406 
407 	lifn.lifn_family = AF_UNSPEC;
408 	lifn.lifn_flags = 0;
409 
410 	if (ioctl(sock, SIOCGLIFNUM, (char *)&lifn) < 0) {
411 		syslog(LOG_ERR,
412 		"statd:init_hostname, get number of interfaces, error: %m");
413 		close(sock);
414 		return (-1);
415 	}
416 
417 	host_name_count = lifn.lifn_count;
418 
419 	host_name = (char **)malloc(host_name_count * sizeof (char *));
420 	if (host_name == NULL) {
421 		perror("statd -a can't get ip configuration\n");
422 		close(sock);
423 		return (-1);
424 	}
425 	close(sock);
426 	return (0);
427 }
428 
429 int
430 main(int argc, char *argv[])
431 {
432 	int c;
433 	int ppid;
434 	extern char *optarg;
435 	int choice = 0;
436 	struct rlimit rl;
437 	int mode;
438 	int sz;
439 	int connmaxrec = RPC_MAXDATASIZE;
440 
441 	addrix = 0;
442 	pathix = 0;
443 
444 	(void) gethostname(hostname, MAXHOSTNAMELEN);
445 	if (init_hostname() < 0)
446 		exit(1);
447 
448 	while ((c = getopt(argc, argv, "Dd:a:p:r")) != EOF)
449 		switch (c) {
450 		case 'd':
451 			(void) sscanf(optarg, "%d", &debug);
452 			break;
453 		case 'D':
454 			choice = 1;
455 			break;
456 		case 'a':
457 			if (addrix < host_name_count) {
458 				if (strcmp(hostname, optarg) != 0) {
459 					sz = strlen(optarg);
460 					if (sz < MAXHOSTNAMELEN) {
461 						host_name[addrix] =
462 							(char *)xmalloc(sz+1);
463 						if (host_name[addrix] !=
464 						    NULL) {
465 						(void) sscanf(optarg, "%s",
466 							host_name[addrix]);
467 							addrix++;
468 						}
469 					} else
470 					(void) fprintf(stderr,
471 				    "statd: -a name of host is too long.\n");
472 				}
473 			} else
474 				(void) fprintf(stderr,
475 				    "statd: -a exceeding maximum hostnames\n");
476 			break;
477 		case 'p':
478 			if (strlen(optarg) < MAXPATHLEN) {
479 				/* If the path_name array has not yet	   */
480 				/* been malloc'ed, do that.  The array	   */
481 				/* should be big enough to hold all of the */
482 				/* -p options we might have.  An upper	   */
483 				/* bound on the number of -p options is	   */
484 				/* argc/2, because each -p option consumes */
485 				/* two arguments.  Here the upper bound	   */
486 				/* is supposing that all the command line  */
487 				/* arguments are -p options, which would   */
488 				/* actually never be the case.		   */
489 				if (path_name == NULL) {
490 					size_t sz = (argc/2) * sizeof (char *);
491 
492 					path_name = (char **)malloc(sz);
493 					if (path_name == NULL) {
494 						(void) fprintf(stderr,
495 						"statd: malloc failed\n");
496 						exit(1);
497 					}
498 					(void) memset(path_name, 0, sz);
499 				}
500 				path_name[pathix] = optarg;
501 				pathix++;
502 			} else {
503 				(void) fprintf(stderr,
504 				"statd: -p pathname is too long.\n");
505 			}
506 			break;
507 		case 'r':
508 			regfiles_only = 1;
509 			break;
510 		default:
511 			(void) fprintf(stderr,
512 			"statd [-d level] [-D]\n");
513 			return (1);
514 		}
515 
516 	if (choice == 0) {
517 		(void) strcpy(statd_home, home0);
518 		(void) strcpy(CURRENT, current0);
519 		(void) strcpy(BACKUP, backup0);
520 		(void) strcpy(STATE, state0);
521 	} else {
522 		(void) strcpy(statd_home, home1);
523 		(void) strcpy(CURRENT, current1);
524 		(void) strcpy(BACKUP, backup1);
525 		(void) strcpy(STATE, state1);
526 	}
527 	if (debug)
528 		(void) printf("debug is on, create entry: %s, %s, %s\n",
529 			CURRENT, BACKUP, STATE);
530 
531 	if (getrlimit(RLIMIT_NOFILE, &rl))
532 		(void) printf("statd: getrlimit failed. \n");
533 
534 	/* Set maxfdlimit current soft limit */
535 	rl.rlim_cur = MAX_FDS;
536 	if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
537 		syslog(LOG_ERR, "statd: unable to set RLIMIT_NOFILE to %d\n",
538 			MAX_FDS);
539 
540 	if (!debug) {
541 		ppid = fork();
542 		if (ppid == -1) {
543 			(void) fprintf(stderr, "statd: fork failure\n");
544 			(void) fflush(stderr);
545 			abort();
546 		}
547 		if (ppid != 0) {
548 			exit(0);
549 		}
550 		closefrom(0);
551 		(void) open("/dev/null", O_RDONLY);
552 		(void) open("/dev/null", O_WRONLY);
553 		(void) dup(1);
554 		(void) setsid();
555 		openlog("statd", LOG_PID, LOG_DAEMON);
556 	}
557 
558 	(void) _create_daemon_lock(STATD, DAEMON_UID, DAEMON_GID);
559 	/*
560 	 * establish our lock on the lock file and write our pid to it.
561 	 * exit if some other process holds the lock, or if there's any
562 	 * error in writing/locking the file.
563 	 */
564 	ppid = _enter_daemon_lock(STATD);
565 	switch (ppid) {
566 	case 0:
567 		break;
568 	case -1:
569 		syslog(LOG_ERR, "error locking for %s: %s", STATD,
570 		    strerror(errno));
571 		exit(2);
572 	default:
573 		/* daemon was already running */
574 		exit(0);
575 	}
576 
577 	/* Get other aliases from each interface. */
578 	merge_hosts();
579 
580 	/*
581 	 * Set to automatic mode such that threads are automatically
582 	 * created
583 	 */
584 	mode = RPC_SVC_MT_AUTO;
585 	if (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {
586 		syslog(LOG_ERR,
587 			"statd:unable to set automatic MT mode.");
588 		exit(1);
589 	}
590 
591 	/*
592 	 * Set non-blocking mode and maximum record size for
593 	 * connection oriented RPC transports.
594 	 */
595 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
596 		syslog(LOG_INFO, "unable to set maximum RPC record size");
597 	}
598 
599 	if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "netpath")) {
600 	    syslog(LOG_ERR,
601 		"statd: unable to create (SM_PROG, SM_VERS) for netpath.");
602 	    exit(1);
603 	}
604 
605 	if (!svc_create(sm_prog_1, NSM_ADDR_PROGRAM, NSM_ADDR_V1, "netpath")) {
606 	    syslog(LOG_ERR,
607 	"statd: unable to create (NSM_ADDR_PROGRAM, NSM_ADDR_V1) for netpath.");
608 	}
609 
610 	/*
611 	 * Make sure /var/statmon and any alternate (-p) statmon
612 	 * directories exist and are owned by daemon.  Then change our uid
613 	 * to daemon.  The uid change is to prevent attacks against local
614 	 * daemons that trust any call from a local root process.
615 	 */
616 
617 	set_statmon_owner();
618 
619 	/*
620 	 *
621 	 * statd now runs as a daemon rather than root and can not
622 	 * dump core under / because of the permission. It is
623 	 * important that current working directory of statd be
624 	 * changed to writable directory /var/statmon so that it
625 	 * can dump the core upon the receipt of the signal.
626 	 * One still need to set allow_setid_core to non-zero in
627 	 * /etc/system to get the core dump.
628 	 *
629 	 */
630 
631 	if (chdir(statd_home) < 0) {
632 		syslog(LOG_ERR, "can't chdir %s: %m", statd_home);
633 		exit(1);
634 	}
635 
636 	copy_client_names();
637 
638 	rwlock_init(&thr_rwlock, USYNC_THREAD, NULL);
639 	mutex_init(&crash_lock, USYNC_THREAD, NULL);
640 	mutex_init(&name_addrlock, USYNC_THREAD, NULL);
641 	cond_init(&crash_finish, USYNC_THREAD, NULL);
642 	cond_init(&retrywait, USYNC_THREAD, NULL);
643 	sm_inithash();
644 	die = 0;
645 	/*
646 	 * This variable is set to ensure that an sm_crash
647 	 * request will not be done at the same time
648 	 * when a statd_init is being done, since sm_crash
649 	 * can reset some variables that statd_init will be using.
650 	 */
651 	in_crash = 1;
652 	statd_init();
653 
654 	if (debug)
655 		(void) printf("Starting svc_run\n");
656 	svc_run();
657 	syslog(LOG_ERR, "statd: svc_run returned\n");
658 	/* NOTREACHED */
659 	thr_exit((void *) 1);
660 	return (0);
661 
662 }
663 
664 /*
665  * Make sure the ownership of the statmon directories is correct, then
666  * change our uid to match.  If the top-level directories (/var/statmon, -p
667  * arguments) don't exist, they are created first.  The sm and sm.bak
668  * directories are not created here, but if they already exist, they are
669  * chowned to the correct uid, along with anything else in the
670  * directories.
671  */
672 
673 static void
674 set_statmon_owner()
675 {
676 	int i;
677 
678 	/*
679 	 * Recursively chown/chgrp /var/statmon and the alternate paths,
680 	 * creating them if necessary.
681 	 */
682 	one_statmon_owner(statd_home);
683 	for (i = 0; i < pathix; i++) {
684 		char alt_path[MAXPATHLEN];
685 
686 		snprintf(alt_path, MAXPATHLEN, "%s/statmon", path_name[i]);
687 		one_statmon_owner(alt_path);
688 	}
689 
690 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
691 	    DAEMON_UID, DAEMON_GID, (char *)NULL) == -1) {
692 		syslog(LOG_ERR, "can't run unprivileged: %m");
693 		exit(1);
694 	}
695 
696 	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
697 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
698 }
699 
700 /*
701  * Copy client names from the alternate statmon directories into
702  * /var/statmon.  The top-level (statmon) directories should already
703  * exist, though the sm and sm.bak directories might not.
704  */
705 
706 static void
707 copy_client_names()
708 {
709 	int i;
710 	char buf[MAXPATHLEN+SM_MAXPATHLEN];
711 
712 	/*
713 	 * Copy all clients from alternate paths to /var/statmon/sm
714 	 * Remove the files in alternate directory when copying is done.
715 	 */
716 	for (i = 0; i < pathix; i++) {
717 		/*
718 		 * If the alternate directories do not exist, create it.
719 		 * If they do exist, just do the copy.
720 		 */
721 		snprintf(buf, sizeof (buf), "%s/statmon/sm", path_name[i]);
722 		if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
723 			if (errno != EEXIST) {
724 				syslog(LOG_ERR,
725 					"can't mkdir %s: %m\n", buf);
726 				continue;
727 			}
728 			copydir_from_to(buf, CURRENT);
729 			(void) remove_dir(buf);
730 		}
731 
732 		(void) snprintf(buf, sizeof (buf), "%s/statmon/sm.bak",
733 				path_name[i]);
734 		if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
735 			if (errno != EEXIST) {
736 				syslog(LOG_ERR,
737 					"can't mkdir %s: %m\n", buf);
738 				continue;
739 			}
740 			copydir_from_to(buf, BACKUP);
741 			(void) remove_dir(buf);
742 		}
743 	}
744 }
745 
746 /*
747  * Create the given directory if it doesn't already exist.  Set the user
748  * and group to daemon for the directory and anything under it.
749  */
750 
751 static void
752 one_statmon_owner(const char *dir)
753 {
754 	if ((mkdir(dir, SM_DIRECTORY_MODE)) == -1) {
755 		if (errno != EEXIST) {
756 			syslog(LOG_ERR, "can't mkdir %s: %m",
757 			    dir);
758 			return;
759 		}
760 	}
761 
762 	if (debug)
763 		printf("Setting owner for %s\n", dir);
764 
765 	if (nftw(dir, nftw_owner, MAX_FDS, FTW_PHYS) != 0) {
766 		syslog(LOG_WARNING, "error setting owner for %s: %m",
767 		    dir);
768 	}
769 }
770 
771 /*
772  * Set the user and group to daemon for the given file or directory.  If
773  * it's a directory, also makes sure that it is mode 755.
774  * Generates a syslog message but does not return an error if there were
775  * problems.
776  */
777 
778 /*ARGSUSED3*/
779 static int
780 nftw_owner(const char *path, const struct stat *statp, int info,
781 	struct FTW *ftw)
782 {
783 	if (!(info == FTW_F || info == FTW_D))
784 		return (0);
785 
786 	/*
787 	 * Some older systems might have mode 777 directories.  Fix that.
788 	 */
789 
790 	if (info == FTW_D && (statp->st_mode & (S_IWGRP | S_IWOTH)) != 0) {
791 		mode_t newmode = (statp->st_mode & ~(S_IWGRP | S_IWOTH)) &
792 			S_IAMB;
793 
794 		if (debug)
795 			printf("chmod %03o %s\n", newmode, path);
796 		if (chmod(path, newmode) < 0) {
797 			int error = errno;
798 
799 			syslog(LOG_WARNING, "can't chmod %s to %03o: %m",
800 			    path, newmode);
801 			if (debug)
802 				printf("  FAILED: %s\n", strerror(error));
803 		}
804 	}
805 
806 	/* If already owned by daemon, don't bother changing. */
807 	if (statp->st_uid == DAEMON_UID &&
808 	    statp->st_gid == DAEMON_GID)
809 		return (0);
810 
811 	if (debug)
812 		printf("lchown %s daemon:daemon\n", path);
813 	if (lchown(path, DAEMON_UID, DAEMON_GID) < 0) {
814 		int error = errno;
815 
816 		syslog(LOG_WARNING, "can't chown %s to daemon: %m",
817 		    path);
818 		if (debug)
819 			printf("  FAILED: %s\n", strerror(error));
820 	}
821 
822 	return (0);
823 }
824