xref: /titanic_50/usr/src/cmd/fs.d/nfs/statd/sm_svc.c (revision 60c807700988885656502665e0cf8afd4b4346f7)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * University Copyright- Copyright (c) 1982, 1986, 1988
31  * The Regents of the University of California
32  * All Rights Reserved
33  *
34  * University Acknowledgment- Portions of this document are derived from
35  * software developed by the University of California, Berkeley, and its
36  * contributors.
37  */
38 
39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
40 
41 #include <stdio.h>
42 #include <stdio_ext.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 	(void) enable_extended_FILE_stdio(-1, -1);
541 
542 	if (!debug) {
543 		ppid = fork();
544 		if (ppid == -1) {
545 			(void) fprintf(stderr, "statd: fork failure\n");
546 			(void) fflush(stderr);
547 			abort();
548 		}
549 		if (ppid != 0) {
550 			exit(0);
551 		}
552 		closefrom(0);
553 		(void) open("/dev/null", O_RDONLY);
554 		(void) open("/dev/null", O_WRONLY);
555 		(void) dup(1);
556 		(void) setsid();
557 		openlog("statd", LOG_PID, LOG_DAEMON);
558 	}
559 
560 	(void) _create_daemon_lock(STATD, DAEMON_UID, DAEMON_GID);
561 	/*
562 	 * establish our lock on the lock file and write our pid to it.
563 	 * exit if some other process holds the lock, or if there's any
564 	 * error in writing/locking the file.
565 	 */
566 	ppid = _enter_daemon_lock(STATD);
567 	switch (ppid) {
568 	case 0:
569 		break;
570 	case -1:
571 		syslog(LOG_ERR, "error locking for %s: %s", STATD,
572 		    strerror(errno));
573 		exit(2);
574 	default:
575 		/* daemon was already running */
576 		exit(0);
577 	}
578 
579 	/* Get other aliases from each interface. */
580 	merge_hosts();
581 
582 	/*
583 	 * Set to automatic mode such that threads are automatically
584 	 * created
585 	 */
586 	mode = RPC_SVC_MT_AUTO;
587 	if (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {
588 		syslog(LOG_ERR,
589 			"statd:unable to set automatic MT mode.");
590 		exit(1);
591 	}
592 
593 	/*
594 	 * Set non-blocking mode and maximum record size for
595 	 * connection oriented RPC transports.
596 	 */
597 	if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
598 		syslog(LOG_INFO, "unable to set maximum RPC record size");
599 	}
600 
601 	if (!svc_create(sm_prog_1, SM_PROG, SM_VERS, "netpath")) {
602 	    syslog(LOG_ERR,
603 		"statd: unable to create (SM_PROG, SM_VERS) for netpath.");
604 	    exit(1);
605 	}
606 
607 	if (!svc_create(sm_prog_1, NSM_ADDR_PROGRAM, NSM_ADDR_V1, "netpath")) {
608 	    syslog(LOG_ERR,
609 	"statd: unable to create (NSM_ADDR_PROGRAM, NSM_ADDR_V1) for netpath.");
610 	}
611 
612 	/*
613 	 * Make sure /var/statmon and any alternate (-p) statmon
614 	 * directories exist and are owned by daemon.  Then change our uid
615 	 * to daemon.  The uid change is to prevent attacks against local
616 	 * daemons that trust any call from a local root process.
617 	 */
618 
619 	set_statmon_owner();
620 
621 	/*
622 	 *
623 	 * statd now runs as a daemon rather than root and can not
624 	 * dump core under / because of the permission. It is
625 	 * important that current working directory of statd be
626 	 * changed to writable directory /var/statmon so that it
627 	 * can dump the core upon the receipt of the signal.
628 	 * One still need to set allow_setid_core to non-zero in
629 	 * /etc/system to get the core dump.
630 	 *
631 	 */
632 
633 	if (chdir(statd_home) < 0) {
634 		syslog(LOG_ERR, "can't chdir %s: %m", statd_home);
635 		exit(1);
636 	}
637 
638 	copy_client_names();
639 
640 	rwlock_init(&thr_rwlock, USYNC_THREAD, NULL);
641 	mutex_init(&crash_lock, USYNC_THREAD, NULL);
642 	mutex_init(&name_addrlock, USYNC_THREAD, NULL);
643 	cond_init(&crash_finish, USYNC_THREAD, NULL);
644 	cond_init(&retrywait, USYNC_THREAD, NULL);
645 	sm_inithash();
646 	die = 0;
647 	/*
648 	 * This variable is set to ensure that an sm_crash
649 	 * request will not be done at the same time
650 	 * when a statd_init is being done, since sm_crash
651 	 * can reset some variables that statd_init will be using.
652 	 */
653 	in_crash = 1;
654 	statd_init();
655 
656 	if (debug)
657 		(void) printf("Starting svc_run\n");
658 	svc_run();
659 	syslog(LOG_ERR, "statd: svc_run returned\n");
660 	/* NOTREACHED */
661 	thr_exit((void *) 1);
662 	return (0);
663 
664 }
665 
666 /*
667  * Make sure the ownership of the statmon directories is correct, then
668  * change our uid to match.  If the top-level directories (/var/statmon, -p
669  * arguments) don't exist, they are created first.  The sm and sm.bak
670  * directories are not created here, but if they already exist, they are
671  * chowned to the correct uid, along with anything else in the
672  * directories.
673  */
674 
675 static void
676 set_statmon_owner(void)
677 {
678 	int i;
679 	boolean_t can_do_mlp;
680 
681 	/*
682 	 * Recursively chown/chgrp /var/statmon and the alternate paths,
683 	 * creating them if necessary.
684 	 */
685 	one_statmon_owner(statd_home);
686 	for (i = 0; i < pathix; i++) {
687 		char alt_path[MAXPATHLEN];
688 
689 		snprintf(alt_path, MAXPATHLEN, "%s/statmon", path_name[i]);
690 		one_statmon_owner(alt_path);
691 	}
692 
693 	can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
694 	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
695 	    DAEMON_UID, DAEMON_GID, can_do_mlp ? PRIV_NET_BINDMLP : NULL,
696 	    NULL) == -1) {
697 		syslog(LOG_ERR, "can't run unprivileged: %m");
698 		exit(1);
699 	}
700 
701 	__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION,
702 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);
703 }
704 
705 /*
706  * Copy client names from the alternate statmon directories into
707  * /var/statmon.  The top-level (statmon) directories should already
708  * exist, though the sm and sm.bak directories might not.
709  */
710 
711 static void
712 copy_client_names()
713 {
714 	int i;
715 	char buf[MAXPATHLEN+SM_MAXPATHLEN];
716 
717 	/*
718 	 * Copy all clients from alternate paths to /var/statmon/sm
719 	 * Remove the files in alternate directory when copying is done.
720 	 */
721 	for (i = 0; i < pathix; i++) {
722 		/*
723 		 * If the alternate directories do not exist, create it.
724 		 * If they do exist, just do the copy.
725 		 */
726 		snprintf(buf, sizeof (buf), "%s/statmon/sm", path_name[i]);
727 		if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
728 			if (errno != EEXIST) {
729 				syslog(LOG_ERR,
730 					"can't mkdir %s: %m\n", buf);
731 				continue;
732 			}
733 			copydir_from_to(buf, CURRENT);
734 			(void) remove_dir(buf);
735 		}
736 
737 		(void) snprintf(buf, sizeof (buf), "%s/statmon/sm.bak",
738 				path_name[i]);
739 		if ((mkdir(buf, SM_DIRECTORY_MODE)) == -1) {
740 			if (errno != EEXIST) {
741 				syslog(LOG_ERR,
742 					"can't mkdir %s: %m\n", buf);
743 				continue;
744 			}
745 			copydir_from_to(buf, BACKUP);
746 			(void) remove_dir(buf);
747 		}
748 	}
749 }
750 
751 /*
752  * Create the given directory if it doesn't already exist.  Set the user
753  * and group to daemon for the directory and anything under it.
754  */
755 
756 static void
757 one_statmon_owner(const char *dir)
758 {
759 	if ((mkdir(dir, SM_DIRECTORY_MODE)) == -1) {
760 		if (errno != EEXIST) {
761 			syslog(LOG_ERR, "can't mkdir %s: %m",
762 			    dir);
763 			return;
764 		}
765 	}
766 
767 	if (debug)
768 		printf("Setting owner for %s\n", dir);
769 
770 	if (nftw(dir, nftw_owner, MAX_FDS, FTW_PHYS) != 0) {
771 		syslog(LOG_WARNING, "error setting owner for %s: %m",
772 		    dir);
773 	}
774 }
775 
776 /*
777  * Set the user and group to daemon for the given file or directory.  If
778  * it's a directory, also makes sure that it is mode 755.
779  * Generates a syslog message but does not return an error if there were
780  * problems.
781  */
782 
783 /*ARGSUSED3*/
784 static int
785 nftw_owner(const char *path, const struct stat *statp, int info,
786 	struct FTW *ftw)
787 {
788 	if (!(info == FTW_F || info == FTW_D))
789 		return (0);
790 
791 	/*
792 	 * Some older systems might have mode 777 directories.  Fix that.
793 	 */
794 
795 	if (info == FTW_D && (statp->st_mode & (S_IWGRP | S_IWOTH)) != 0) {
796 		mode_t newmode = (statp->st_mode & ~(S_IWGRP | S_IWOTH)) &
797 			S_IAMB;
798 
799 		if (debug)
800 			printf("chmod %03o %s\n", newmode, path);
801 		if (chmod(path, newmode) < 0) {
802 			int error = errno;
803 
804 			syslog(LOG_WARNING, "can't chmod %s to %03o: %m",
805 			    path, newmode);
806 			if (debug)
807 				printf("  FAILED: %s\n", strerror(error));
808 		}
809 	}
810 
811 	/* If already owned by daemon, don't bother changing. */
812 	if (statp->st_uid == DAEMON_UID &&
813 	    statp->st_gid == DAEMON_GID)
814 		return (0);
815 
816 	if (debug)
817 		printf("lchown %s daemon:daemon\n", path);
818 	if (lchown(path, DAEMON_UID, DAEMON_GID) < 0) {
819 		int error = errno;
820 
821 		syslog(LOG_WARNING, "can't chown %s to daemon: %m",
822 		    path);
823 		if (debug)
824 			printf("  FAILED: %s\n", strerror(error));
825 	}
826 
827 	return (0);
828 }
829