xref: /dragonfly/sbin/hammer/cmd_cleanup.c (revision 574066d3)
16a6e350fSMatthew Dillon /*
26a6e350fSMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
36a6e350fSMatthew Dillon  *
46a6e350fSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
56a6e350fSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
66a6e350fSMatthew Dillon  *
76a6e350fSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
86a6e350fSMatthew Dillon  * modification, are permitted provided that the following conditions
96a6e350fSMatthew Dillon  * are met:
106a6e350fSMatthew Dillon  *
116a6e350fSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
126a6e350fSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
136a6e350fSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
146a6e350fSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
156a6e350fSMatthew Dillon  *    the documentation and/or other materials provided with the
166a6e350fSMatthew Dillon  *    distribution.
176a6e350fSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
186a6e350fSMatthew Dillon  *    contributors may be used to endorse or promote products derived
196a6e350fSMatthew Dillon  *    from this software without specific, prior written permission.
206a6e350fSMatthew Dillon  *
216a6e350fSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
226a6e350fSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
236a6e350fSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
246a6e350fSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
256a6e350fSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
266a6e350fSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
276a6e350fSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
286a6e350fSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
296a6e350fSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
306a6e350fSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
316a6e350fSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326a6e350fSMatthew Dillon  * SUCH DAMAGE.
336a6e350fSMatthew Dillon  *
34bb8e52c0SThomas Nikolajsen  * $DragonFly: src/sbin/hammer/cmd_cleanup.c,v 1.6 2008/10/07 22:28:41 thomas Exp $
356a6e350fSMatthew Dillon  */
366a6e350fSMatthew Dillon /*
376a6e350fSMatthew Dillon  * Clean up a specific HAMMER filesystem or all HAMMER filesystems.
386a6e350fSMatthew Dillon  *
396a6e350fSMatthew Dillon  * Each filesystem is expected to have a <mount>/snapshots directory.
406a6e350fSMatthew Dillon  * No cleanup will be performed on any filesystem that does not.  If
416a6e350fSMatthew Dillon  * no filesystems are specified the 'df' program is run and any HAMMER
426a6e350fSMatthew Dillon  * or null-mounted hammer PFS's are extracted.
436a6e350fSMatthew Dillon  *
446a6e350fSMatthew Dillon  * The snapshots directory may contain a config file called 'config'.  If
456a6e350fSMatthew Dillon  * no config file is present one will be created with the following
466a6e350fSMatthew Dillon  * defaults:
476a6e350fSMatthew Dillon  *
485e435c92SMatthew Dillon  *	snapshots 1d 60d	(0d 0d for /tmp, /var/tmp, /usr/obj)
496a6e350fSMatthew Dillon  *	prune     1d 5m
506a6e350fSMatthew Dillon  *	reblock   1d 5m
516a6e350fSMatthew Dillon  *	recopy    30d 5m
526a6e350fSMatthew Dillon  *
536a6e350fSMatthew Dillon  * All hammer commands create and maintain cycle files in the snapshots
546a6e350fSMatthew Dillon  * directory.
556a6e350fSMatthew Dillon  */
566a6e350fSMatthew Dillon 
576a6e350fSMatthew Dillon #include "hammer.h"
586a6e350fSMatthew Dillon 
59c453712aSMatthew Dillon struct didpfs {
60c453712aSMatthew Dillon 	struct didpfs *next;
61c453712aSMatthew Dillon 	uuid_t		uuid;
62c453712aSMatthew Dillon };
63c453712aSMatthew Dillon 
646a6e350fSMatthew Dillon static void do_cleanup(const char *path);
656a6e350fSMatthew Dillon static int strtosecs(char *ptr);
666a6e350fSMatthew Dillon static const char *dividing_slash(const char *path);
676a6e350fSMatthew Dillon static int check_period(const char *snapshots_path, const char *cmd, int arg1,
686a6e350fSMatthew Dillon 			time_t *savep);
696a6e350fSMatthew Dillon static void save_period(const char *snapshots_path, const char *cmd,
706a6e350fSMatthew Dillon 			time_t savet);
71c6c298a7SMatthew Dillon static int check_softlinks(const char *snapshots_path);
72ff1c9800SMatthew Dillon static void cleanup_softlinks(const char *path, const char *snapshots_path,
735e435c92SMatthew Dillon 			int arg2, char *arg3);
74ff1c9800SMatthew Dillon static int check_expired(const char *fpath, int arg2);
756a6e350fSMatthew Dillon 
766a6e350fSMatthew Dillon static int cleanup_snapshots(const char *path, const char *snapshots_path,
776a6e350fSMatthew Dillon 			      int arg1, int arg2);
786a6e350fSMatthew Dillon static int cleanup_prune(const char *path, const char *snapshots_path,
79c6c298a7SMatthew Dillon 			      int arg1, int arg2, int snapshots_disabled);
806a6e350fSMatthew Dillon static int cleanup_reblock(const char *path, const char *snapshots_path,
816a6e350fSMatthew Dillon 			      int arg1, int arg2);
826a6e350fSMatthew Dillon static int cleanup_recopy(const char *path, const char *snapshots_path,
836a6e350fSMatthew Dillon 			      int arg1, int arg2);
846a6e350fSMatthew Dillon 
856a6e350fSMatthew Dillon static void runcmd(int *resp, const char *ctl, ...);
866a6e350fSMatthew Dillon 
876a6e350fSMatthew Dillon #define WS	" \t\r\n"
886a6e350fSMatthew Dillon 
89c453712aSMatthew Dillon struct didpfs *FirstPFS;
906a6e350fSMatthew Dillon 
916a6e350fSMatthew Dillon void
926a6e350fSMatthew Dillon hammer_cmd_cleanup(char **av, int ac)
936a6e350fSMatthew Dillon {
946a6e350fSMatthew Dillon 	FILE *fp;
955cb5194dSSascha Wildner 	char *fs, *ptr, *path;
966a6e350fSMatthew Dillon 	char buf[256];
976a6e350fSMatthew Dillon 
986a6e350fSMatthew Dillon 	tzset();
996a6e350fSMatthew Dillon 	if (ac == 0) {
1006ed24421SMatthew Dillon 		fp = popen("/sbin/mount -t hammer,null", "r");
1016a6e350fSMatthew Dillon 		if (fp == NULL)
1025cb5194dSSascha Wildner 			errx(1, "hammer cleanup: 'mount' failed");
1036a6e350fSMatthew Dillon 		while (fgets(buf, sizeof(buf), fp) != NULL) {
1045cb5194dSSascha Wildner 			fs = strtok(buf, WS);
1055cb5194dSSascha Wildner 			if (fs == NULL)
1066a6e350fSMatthew Dillon 				continue;
1076a6e350fSMatthew Dillon 			ptr = strtok(NULL, WS);
1085cb5194dSSascha Wildner 			if (ptr == NULL)
1095cb5194dSSascha Wildner 				continue;
1106a6e350fSMatthew Dillon 			path = strtok(NULL, WS);
1115cb5194dSSascha Wildner 			if (path == NULL)
1125cb5194dSSascha Wildner 				continue;
1135cb5194dSSascha Wildner 			ptr = strtok(NULL, WS);
1145cb5194dSSascha Wildner 			if (ptr == NULL)
1155cb5194dSSascha Wildner 				continue;
1165cb5194dSSascha Wildner 			if ((strncmp(ptr, "(hammer,", 8) == 0) ||
1175cb5194dSSascha Wildner 			    ((strncmp(ptr, "(null,", 6) == 0) &&
118*574066d3SMatthew Dillon 			     (strstr(fs, "/@@0x") != NULL))) {
1196a6e350fSMatthew Dillon 				do_cleanup(path);
1206a6e350fSMatthew Dillon 			}
121*574066d3SMatthew Dillon 		}
1226a6e350fSMatthew Dillon 		fclose(fp);
1236a6e350fSMatthew Dillon 	} else {
1246a6e350fSMatthew Dillon 		while (ac) {
1256a6e350fSMatthew Dillon 			do_cleanup(*av);
1266a6e350fSMatthew Dillon 			--ac;
1276a6e350fSMatthew Dillon 			++av;
1286a6e350fSMatthew Dillon 		}
1296a6e350fSMatthew Dillon 	}
1306a6e350fSMatthew Dillon }
1316a6e350fSMatthew Dillon 
1326a6e350fSMatthew Dillon static
1336a6e350fSMatthew Dillon void
1346a6e350fSMatthew Dillon do_cleanup(const char *path)
1356a6e350fSMatthew Dillon {
1366a6e350fSMatthew Dillon 	struct hammer_ioc_pseudofs_rw pfs;
1376a6e350fSMatthew Dillon 	union hammer_ioc_mrecord_any mrec_tmp;
1386a6e350fSMatthew Dillon 	char *snapshots_path;
1396a6e350fSMatthew Dillon 	char *config_path;
1406a6e350fSMatthew Dillon 	struct stat st;
1416a6e350fSMatthew Dillon 	char *cmd;
1426a6e350fSMatthew Dillon 	char *ptr;
1436a6e350fSMatthew Dillon 	int arg1;
1446a6e350fSMatthew Dillon 	int arg2;
1455e435c92SMatthew Dillon 	char *arg3;
1466a6e350fSMatthew Dillon 	time_t savet;
1476a6e350fSMatthew Dillon 	char buf[256];
1486a6e350fSMatthew Dillon 	FILE *fp;
149c453712aSMatthew Dillon 	struct didpfs *didpfs;
150c6c298a7SMatthew Dillon 	int snapshots_disabled = 0;
151c6c298a7SMatthew Dillon 	int prune_warning = 0;
1526a6e350fSMatthew Dillon 	int fd;
1536a6e350fSMatthew Dillon 	int r;
1546a6e350fSMatthew Dillon 
1556a6e350fSMatthew Dillon 	bzero(&pfs, sizeof(pfs));
1566a6e350fSMatthew Dillon 	bzero(&mrec_tmp, sizeof(mrec_tmp));
1576a6e350fSMatthew Dillon 	pfs.ondisk = &mrec_tmp.pfs.pfsd;
1586a6e350fSMatthew Dillon 	pfs.bytes = sizeof(mrec_tmp.pfs.pfsd);
1596a6e350fSMatthew Dillon 	pfs.pfs_id = -1;
1606a6e350fSMatthew Dillon 
1616a6e350fSMatthew Dillon 	printf("cleanup %-20s -", path);
1626a6e350fSMatthew Dillon 	fd = open(path, O_RDONLY);
1636a6e350fSMatthew Dillon 	if (fd < 0) {
1646a6e350fSMatthew Dillon 		printf(" unable to access directory: %s\n", strerror(errno));
1656a6e350fSMatthew Dillon 		return;
1666a6e350fSMatthew Dillon 	}
1676a6e350fSMatthew Dillon 	if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) != 0) {
1686a6e350fSMatthew Dillon 		printf(" not a HAMMER filesystem: %s\n", strerror(errno));
1696a6e350fSMatthew Dillon 		return;
1706a6e350fSMatthew Dillon 	}
1716a6e350fSMatthew Dillon 	close(fd);
1726a6e350fSMatthew Dillon 	if (pfs.version != HAMMER_IOC_PSEUDOFS_VERSION) {
1736a6e350fSMatthew Dillon 		printf(" unrecognized HAMMER version\n");
1746a6e350fSMatthew Dillon 		return;
1756a6e350fSMatthew Dillon 	}
1766a6e350fSMatthew Dillon 
1776a6e350fSMatthew Dillon 	/*
1786a6e350fSMatthew Dillon 	 * Make sure we have not already handled this PFS.  Several nullfs
1796a6e350fSMatthew Dillon 	 * mounts might alias the same PFS.
1806a6e350fSMatthew Dillon 	 */
181c453712aSMatthew Dillon 	for (didpfs = FirstPFS; didpfs; didpfs = didpfs->next) {
182c453712aSMatthew Dillon 		if (bcmp(&didpfs->uuid, &mrec_tmp.pfs.pfsd.unique_uuid, sizeof(uuid_t)) == 0) {
183bb8e52c0SThomas Nikolajsen 			printf(" PFS #%d already handled\n", pfs.pfs_id);
1846a6e350fSMatthew Dillon 			return;
1856a6e350fSMatthew Dillon 		}
186c453712aSMatthew Dillon 	}
187c453712aSMatthew Dillon 	didpfs = malloc(sizeof(*didpfs));
188c453712aSMatthew Dillon 	didpfs->next = FirstPFS;
189c453712aSMatthew Dillon 	FirstPFS = didpfs;
190c453712aSMatthew Dillon 	didpfs->uuid = mrec_tmp.pfs.pfsd.unique_uuid;
1916a6e350fSMatthew Dillon 
1926a6e350fSMatthew Dillon 	/*
193ff1c9800SMatthew Dillon 	 * Figure out where the snapshot directory is.
194ff1c9800SMatthew Dillon 	 */
195ff1c9800SMatthew Dillon 	if (mrec_tmp.pfs.pfsd.snapshots[0] == '/') {
196ff1c9800SMatthew Dillon 		asprintf(&snapshots_path, "%s", mrec_tmp.pfs.pfsd.snapshots);
197ff1c9800SMatthew Dillon 	} else if (mrec_tmp.pfs.pfsd.snapshots[0]) {
198ff1c9800SMatthew Dillon 		printf(" WARNING: pfs-slave's snapshots dir is not absolute\n");
199ff1c9800SMatthew Dillon 		return;
200ff1c9800SMatthew Dillon 	} else if (mrec_tmp.pfs.pfsd.mirror_flags & HAMMER_PFSD_SLAVE) {
201ff1c9800SMatthew Dillon 		printf(" WARNING: must configure snapshot dir for PFS slave\n");
202ff1c9800SMatthew Dillon 		printf("\tWe suggest <fs>/var/slaves/<name> where "
203ff1c9800SMatthew Dillon 		       "<fs> is the base HAMMER fs\n");
204bb8e52c0SThomas Nikolajsen 		printf("\tcontaining the slave\n");
205ff1c9800SMatthew Dillon 		return;
206ff1c9800SMatthew Dillon 	} else {
207ff1c9800SMatthew Dillon 		asprintf(&snapshots_path,
208ff1c9800SMatthew Dillon 			 "%s%ssnapshots", path, dividing_slash(path));
209ff1c9800SMatthew Dillon 	}
210ff1c9800SMatthew Dillon 
211ff1c9800SMatthew Dillon 	/*
2126a6e350fSMatthew Dillon 	 * Create a snapshot directory if necessary, and a config file if
2136a6e350fSMatthew Dillon 	 * necessary.
2146a6e350fSMatthew Dillon 	 */
2156a6e350fSMatthew Dillon 	if (stat(snapshots_path, &st) < 0) {
2166a6e350fSMatthew Dillon 		if (mkdir(snapshots_path, 0755) != 0) {
2176a6e350fSMatthew Dillon 			free(snapshots_path);
218ff1c9800SMatthew Dillon 			printf(" unable to create snapshot dir \"%s\": %s\n",
219ff1c9800SMatthew Dillon 				snapshots_path, strerror(errno));
2206a6e350fSMatthew Dillon 			return;
2216a6e350fSMatthew Dillon 		}
2226a6e350fSMatthew Dillon 	}
2236a6e350fSMatthew Dillon 	asprintf(&config_path, "%s/config", snapshots_path);
2246a6e350fSMatthew Dillon 	if ((fp = fopen(config_path, "r")) == NULL) {
2256a6e350fSMatthew Dillon 		fp = fopen(config_path, "w");
2266a6e350fSMatthew Dillon 		if (fp == NULL) {
2276a6e350fSMatthew Dillon 			printf(" cannot create %s: %s\n",
2286a6e350fSMatthew Dillon 				config_path, strerror(errno));
2296a6e350fSMatthew Dillon 			return;
2306a6e350fSMatthew Dillon 		}
2316a6e350fSMatthew Dillon 		if (strcmp(path, "/tmp") == 0 ||
2326a6e350fSMatthew Dillon 		    strcmp(path, "/var/tmp") == 0 ||
2336a6e350fSMatthew Dillon 		    strcmp(path, "/usr/obj") == 0) {
2345e435c92SMatthew Dillon 			fprintf(fp, "snapshots 0d 0d\n");
2356a6e350fSMatthew Dillon 		} else {
2366a6e350fSMatthew Dillon 			fprintf(fp, "snapshots 1d 60d\n");
2376a6e350fSMatthew Dillon 		}
2386a6e350fSMatthew Dillon 		fprintf(fp,
2396a6e350fSMatthew Dillon 			"prune     1d 5m\n"
2406a6e350fSMatthew Dillon 			"reblock   1d 5m\n"
2416a6e350fSMatthew Dillon 			"recopy    30d 10m\n");
2426a6e350fSMatthew Dillon 		fclose(fp);
2436a6e350fSMatthew Dillon 		fp = fopen(config_path, "r");
2446a6e350fSMatthew Dillon 	}
2456a6e350fSMatthew Dillon 	if (fp == NULL) {
2466a6e350fSMatthew Dillon 		printf(" cannot access %s: %s\n",
2476a6e350fSMatthew Dillon 		       config_path, strerror(errno));
2486a6e350fSMatthew Dillon 		return;
2496a6e350fSMatthew Dillon 	}
2506a6e350fSMatthew Dillon 
251ff1c9800SMatthew Dillon 	printf(" handle PFS #%d using %s\n", pfs.pfs_id, snapshots_path);
2526a6e350fSMatthew Dillon 
2536a6e350fSMatthew Dillon 	/*
2546a6e350fSMatthew Dillon 	 * Process the config file
2556a6e350fSMatthew Dillon 	 */
2566a6e350fSMatthew Dillon 	while (fgets(buf, sizeof(buf), fp) != NULL) {
2576a6e350fSMatthew Dillon 		cmd = strtok(buf, WS);
2586a6e350fSMatthew Dillon 		arg1 = 0;
2596a6e350fSMatthew Dillon 		arg2 = 0;
2605e435c92SMatthew Dillon 		arg3 = NULL;
2616a6e350fSMatthew Dillon 		if ((ptr = strtok(NULL, WS)) != NULL) {
2626a6e350fSMatthew Dillon 			arg1 = strtosecs(ptr);
2635e435c92SMatthew Dillon 			if ((ptr = strtok(NULL, WS)) != NULL) {
2646a6e350fSMatthew Dillon 				arg2 = strtosecs(ptr);
2655e435c92SMatthew Dillon 				arg3 = strtok(NULL, WS);
2665e435c92SMatthew Dillon 			}
2676a6e350fSMatthew Dillon 		}
2686a6e350fSMatthew Dillon 
2696a6e350fSMatthew Dillon 		printf("%20s - ", cmd);
2706a6e350fSMatthew Dillon 		fflush(stdout);
2716a6e350fSMatthew Dillon 
2726a6e350fSMatthew Dillon 		r = 1;
2736a6e350fSMatthew Dillon 		if (strcmp(cmd, "snapshots") == 0) {
2745e435c92SMatthew Dillon 			if (arg1 == 0) {
2755e435c92SMatthew Dillon 				if (arg2 && check_softlinks(snapshots_path)) {
2765e435c92SMatthew Dillon 					printf("only removing old snapshots\n");
2775e435c92SMatthew Dillon 					prune_warning = 1;
2785e435c92SMatthew Dillon 					cleanup_softlinks(path, snapshots_path,
2795e435c92SMatthew Dillon 							  arg2, arg3);
2805e435c92SMatthew Dillon 				} else {
2815e435c92SMatthew Dillon 					printf("disabled\n");
2825e435c92SMatthew Dillon 					snapshots_disabled = 1;
2835e435c92SMatthew Dillon 				}
2845e435c92SMatthew Dillon 			} else
2856a6e350fSMatthew Dillon 			if (check_period(snapshots_path, cmd, arg1, &savet)) {
2866a6e350fSMatthew Dillon 				printf("run\n");
2875e435c92SMatthew Dillon 				cleanup_softlinks(path, snapshots_path,
2885e435c92SMatthew Dillon 						  arg2, arg3);
2896a6e350fSMatthew Dillon 				r = cleanup_snapshots(path, snapshots_path,
2906a6e350fSMatthew Dillon 						  arg1, arg2);
2916a6e350fSMatthew Dillon 			} else {
2926a6e350fSMatthew Dillon 				printf("skip\n");
2936a6e350fSMatthew Dillon 			}
2945e435c92SMatthew Dillon 		} else if (arg1 == 0) {
2955e435c92SMatthew Dillon 			/*
2965e435c92SMatthew Dillon 			 * The commands following this check can't handle
2975e435c92SMatthew Dillon 			 * a period of 0, so call the feature disabled and
2985e435c92SMatthew Dillon 			 * ignore the directive.
2995e435c92SMatthew Dillon 			 */
3005e435c92SMatthew Dillon 			printf("disabled\n");
3016a6e350fSMatthew Dillon 		} else if (strcmp(cmd, "prune") == 0) {
3026a6e350fSMatthew Dillon 			if (check_period(snapshots_path, cmd, arg1, &savet)) {
3035e435c92SMatthew Dillon 				if (prune_warning) {
3045e435c92SMatthew Dillon 					printf("run - WARNING snapshot "
3055e435c92SMatthew Dillon 					       "softlinks present "
306226f3799SThomas Nikolajsen 					       "but snapshots disabled\n");
3075e435c92SMatthew Dillon 				} else {
308c6c298a7SMatthew Dillon 					printf("run\n");
3095e435c92SMatthew Dillon 				}
3106a6e350fSMatthew Dillon 				r = cleanup_prune(path, snapshots_path,
311c6c298a7SMatthew Dillon 					      arg1, arg2, snapshots_disabled);
3126a6e350fSMatthew Dillon 			} else {
3136a6e350fSMatthew Dillon 				printf("skip\n");
3146a6e350fSMatthew Dillon 			}
3156a6e350fSMatthew Dillon 		} else if (strcmp(cmd, "reblock") == 0) {
3166a6e350fSMatthew Dillon 			if (check_period(snapshots_path, cmd, arg1, &savet)) {
3176a6e350fSMatthew Dillon 				printf("run");
3186a6e350fSMatthew Dillon 				fflush(stdout);
3196a6e350fSMatthew Dillon 				if (VerboseOpt)
3206a6e350fSMatthew Dillon 					printf("\n");
3216a6e350fSMatthew Dillon 				r = cleanup_reblock(path, snapshots_path,
3226a6e350fSMatthew Dillon 						arg1, arg2);
3236a6e350fSMatthew Dillon 			} else {
3246a6e350fSMatthew Dillon 				printf("skip\n");
3256a6e350fSMatthew Dillon 			}
3266a6e350fSMatthew Dillon 		} else if (strcmp(cmd, "recopy") == 0) {
3276a6e350fSMatthew Dillon 			if (check_period(snapshots_path, cmd, arg1, &savet)) {
3286a6e350fSMatthew Dillon 				printf("run");
3296a6e350fSMatthew Dillon 				fflush(stdout);
3306a6e350fSMatthew Dillon 				if (VerboseOpt)
3316a6e350fSMatthew Dillon 					printf("\n");
3326a6e350fSMatthew Dillon 				r = cleanup_recopy(path, snapshots_path,
3336a6e350fSMatthew Dillon 					       arg1, arg2);
3346a6e350fSMatthew Dillon 			} else {
3356a6e350fSMatthew Dillon 				printf("skip\n");
3366a6e350fSMatthew Dillon 			}
3376a6e350fSMatthew Dillon 		} else {
3386a6e350fSMatthew Dillon 			printf("unknown directive\n");
3396a6e350fSMatthew Dillon 			r = 1;
3406a6e350fSMatthew Dillon 		}
3416a6e350fSMatthew Dillon 		if (r == 0)
3426a6e350fSMatthew Dillon 			save_period(snapshots_path, cmd, savet);
3436a6e350fSMatthew Dillon 	}
3446a6e350fSMatthew Dillon 	fclose(fp);
3456a6e350fSMatthew Dillon 	usleep(1000);
3466a6e350fSMatthew Dillon }
3476a6e350fSMatthew Dillon 
3486a6e350fSMatthew Dillon static
3496a6e350fSMatthew Dillon int
3506a6e350fSMatthew Dillon strtosecs(char *ptr)
3516a6e350fSMatthew Dillon {
3526a6e350fSMatthew Dillon 	int val;
3536a6e350fSMatthew Dillon 
3546a6e350fSMatthew Dillon 	val = strtol(ptr, &ptr, 0);
3556a6e350fSMatthew Dillon 	switch(*ptr) {
3566a6e350fSMatthew Dillon 	case 'd':
3576a6e350fSMatthew Dillon 		val *= 24;
3586a6e350fSMatthew Dillon 		/* fall through */
3596a6e350fSMatthew Dillon 	case 'h':
3606a6e350fSMatthew Dillon 		val *= 60;
3616a6e350fSMatthew Dillon 		/* fall through */
3626a6e350fSMatthew Dillon 	case 'm':
3636a6e350fSMatthew Dillon 		val *= 60;
3646a6e350fSMatthew Dillon 		/* fall through */
3656a6e350fSMatthew Dillon 	case 's':
3666a6e350fSMatthew Dillon 		break;
3676a6e350fSMatthew Dillon 	default:
3686a6e350fSMatthew Dillon 		errx(1, "illegal suffix converting %s\n", ptr);
3696a6e350fSMatthew Dillon 		break;
3706a6e350fSMatthew Dillon 	}
3716a6e350fSMatthew Dillon 	return(val);
3726a6e350fSMatthew Dillon }
3736a6e350fSMatthew Dillon 
3746a6e350fSMatthew Dillon static const char *
3756a6e350fSMatthew Dillon dividing_slash(const char *path)
3766a6e350fSMatthew Dillon {
3776a6e350fSMatthew Dillon 	int len = strlen(path);
3786a6e350fSMatthew Dillon 	if (len && path[len-1] == '/')
3796a6e350fSMatthew Dillon 		return("");
3806a6e350fSMatthew Dillon 	else
3816a6e350fSMatthew Dillon 		return("/");
3826a6e350fSMatthew Dillon }
3836a6e350fSMatthew Dillon 
3846a6e350fSMatthew Dillon /*
3856a6e350fSMatthew Dillon  * Check whether the desired period has elapsed since the last successful
3866a6e350fSMatthew Dillon  * run.  The run may take a while and cross a boundary so we remember the
3876a6e350fSMatthew Dillon  * current time_t so we can save it later on.
3886a6e350fSMatthew Dillon  *
3896a6e350fSMatthew Dillon  * Periods in minutes, hours, or days are assumed to have been crossed
3906a6e350fSMatthew Dillon  * if the local time crosses a minute, hour, or day boundary regardless
3916a6e350fSMatthew Dillon  * of how close the last operation actually was.
3926a6e350fSMatthew Dillon  */
3936a6e350fSMatthew Dillon static int
3946a6e350fSMatthew Dillon check_period(const char *snapshots_path, const char *cmd, int arg1,
3956a6e350fSMatthew Dillon 	time_t *savep)
3966a6e350fSMatthew Dillon {
3976a6e350fSMatthew Dillon 	char *check_path;
3986a6e350fSMatthew Dillon 	struct tm tp1;
3996a6e350fSMatthew Dillon 	struct tm tp2;
4006a6e350fSMatthew Dillon 	FILE *fp;
4016a6e350fSMatthew Dillon 	time_t baset, lastt;
4026a6e350fSMatthew Dillon 	char buf[256];
4036a6e350fSMatthew Dillon 
4046a6e350fSMatthew Dillon 	time(savep);
4056a6e350fSMatthew Dillon 	localtime_r(savep, &tp1);
4066a6e350fSMatthew Dillon 
4076a6e350fSMatthew Dillon 	/*
4086a6e350fSMatthew Dillon 	 * Retrieve the start time of the last successful operation.
4096a6e350fSMatthew Dillon 	 */
4106a6e350fSMatthew Dillon 	asprintf(&check_path, "%s/.%s.period", snapshots_path, cmd);
4116a6e350fSMatthew Dillon 	fp = fopen(check_path, "r");
4126a6e350fSMatthew Dillon 	free(check_path);
4136a6e350fSMatthew Dillon 	if (fp == NULL)
4146a6e350fSMatthew Dillon 		return(1);
4156a6e350fSMatthew Dillon 	if (fgets(buf, sizeof(buf), fp) == NULL) {
4166a6e350fSMatthew Dillon 		fclose(fp);
4176a6e350fSMatthew Dillon 		return(1);
4186a6e350fSMatthew Dillon 	}
4196a6e350fSMatthew Dillon 	fclose(fp);
4206a6e350fSMatthew Dillon 
4216a6e350fSMatthew Dillon 	lastt = strtol(buf, NULL, 0);
4226a6e350fSMatthew Dillon 	localtime_r(&lastt, &tp2);
4236a6e350fSMatthew Dillon 
4246a6e350fSMatthew Dillon 	/*
4256a6e350fSMatthew Dillon 	 * Normalize the times.  e.g. if asked to do something on a 1-day
4266a6e350fSMatthew Dillon 	 * interval the operation will be performed as soon as the day
4276a6e350fSMatthew Dillon 	 * turns over relative to the previous operation, even if the previous
4286a6e350fSMatthew Dillon 	 * operation ran a few seconds ago just before midnight.
4296a6e350fSMatthew Dillon 	 */
4306a6e350fSMatthew Dillon 	if (arg1 % 60 == 0) {
4316a6e350fSMatthew Dillon 		tp1.tm_sec = 0;
4326a6e350fSMatthew Dillon 		tp2.tm_sec = 0;
4336a6e350fSMatthew Dillon 	}
4346a6e350fSMatthew Dillon 	if (arg1 % (60 * 60) == 0) {
4356a6e350fSMatthew Dillon 		tp1.tm_min = 0;
4366a6e350fSMatthew Dillon 		tp2.tm_min = 0;
4376a6e350fSMatthew Dillon 	}
4386a6e350fSMatthew Dillon 	if (arg1 % (24 * 60 * 60) == 0) {
4396a6e350fSMatthew Dillon 		tp1.tm_hour = 0;
4406a6e350fSMatthew Dillon 		tp2.tm_hour = 0;
4416a6e350fSMatthew Dillon 	}
4426a6e350fSMatthew Dillon 
4436a6e350fSMatthew Dillon 	baset = mktime(&tp1);
4446a6e350fSMatthew Dillon 	lastt = mktime(&tp2);
4456a6e350fSMatthew Dillon 
4466a6e350fSMatthew Dillon #if 0
4476a6e350fSMatthew Dillon 	printf("%lld vs %lld\n", (long long)(baset - lastt), (long long)arg1);
4486a6e350fSMatthew Dillon #endif
4496a6e350fSMatthew Dillon 
4506a6e350fSMatthew Dillon 	if ((int)(baset - lastt) >= arg1)
4516a6e350fSMatthew Dillon 		return(1);
4526a6e350fSMatthew Dillon 	return(0);
4536a6e350fSMatthew Dillon }
4546a6e350fSMatthew Dillon 
4556a6e350fSMatthew Dillon /*
4566a6e350fSMatthew Dillon  * Store the start time of the last successful operation.
4576a6e350fSMatthew Dillon  */
4586a6e350fSMatthew Dillon static void
4596a6e350fSMatthew Dillon save_period(const char *snapshots_path, const char *cmd,
4606a6e350fSMatthew Dillon 			time_t savet)
4616a6e350fSMatthew Dillon {
4626a6e350fSMatthew Dillon 	char *ocheck_path;
4636a6e350fSMatthew Dillon 	char *ncheck_path;
4646a6e350fSMatthew Dillon 	FILE *fp;
4656a6e350fSMatthew Dillon 
4666a6e350fSMatthew Dillon 	asprintf(&ocheck_path, "%s/.%s.period", snapshots_path, cmd);
4676a6e350fSMatthew Dillon 	asprintf(&ncheck_path, "%s/.%s.period.new", snapshots_path, cmd);
4686a6e350fSMatthew Dillon 	fp = fopen(ncheck_path, "w");
4696a6e350fSMatthew Dillon 	fprintf(fp, "0x%08llx\n", (long long)savet);
4706a6e350fSMatthew Dillon 	if (fclose(fp) == 0)
4716a6e350fSMatthew Dillon 		rename(ncheck_path, ocheck_path);
4726a6e350fSMatthew Dillon 	remove(ncheck_path);
4736a6e350fSMatthew Dillon }
4746a6e350fSMatthew Dillon 
475ff1c9800SMatthew Dillon /*
476ff1c9800SMatthew Dillon  * Simply count the number of softlinks in the snapshots dir
477ff1c9800SMatthew Dillon  */
478c6c298a7SMatthew Dillon static int
479c6c298a7SMatthew Dillon check_softlinks(const char *snapshots_path)
480c6c298a7SMatthew Dillon {
481c6c298a7SMatthew Dillon 	struct dirent *den;
482c6c298a7SMatthew Dillon 	struct stat st;
483c6c298a7SMatthew Dillon 	DIR *dir;
484c6c298a7SMatthew Dillon 	char *fpath;
485c6c298a7SMatthew Dillon 	int res = 0;
486c6c298a7SMatthew Dillon 
487c6c298a7SMatthew Dillon 	if ((dir = opendir(snapshots_path)) != NULL) {
488c6c298a7SMatthew Dillon 		while ((den = readdir(dir)) != NULL) {
489c6c298a7SMatthew Dillon 			if (den->d_name[0] == '.')
490c6c298a7SMatthew Dillon 				continue;
491c6c298a7SMatthew Dillon 			asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
492c6c298a7SMatthew Dillon 			if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
493c6c298a7SMatthew Dillon 				++res;
494c6c298a7SMatthew Dillon 			free(fpath);
495c6c298a7SMatthew Dillon 		}
496c6c298a7SMatthew Dillon 		closedir(dir);
497c6c298a7SMatthew Dillon 	}
498c6c298a7SMatthew Dillon 	return(res);
499c6c298a7SMatthew Dillon }
500c6c298a7SMatthew Dillon 
5016a6e350fSMatthew Dillon /*
502ff1c9800SMatthew Dillon  * Clean up expired softlinks in the snapshots dir
503ff1c9800SMatthew Dillon  */
504ff1c9800SMatthew Dillon static void
5055e435c92SMatthew Dillon cleanup_softlinks(const char *path __unused, const char *snapshots_path,
5065e435c92SMatthew Dillon 		  int arg2, char *arg3)
507ff1c9800SMatthew Dillon {
508ff1c9800SMatthew Dillon 	struct dirent *den;
509ff1c9800SMatthew Dillon 	struct stat st;
510ff1c9800SMatthew Dillon 	DIR *dir;
511ff1c9800SMatthew Dillon 	char *fpath;
5125e435c92SMatthew Dillon 	int anylink = 0;
5135e435c92SMatthew Dillon 
51409e1b0d6SMatthew Dillon 	if (arg3 != NULL && strstr(arg3, "any") != NULL)
5155e435c92SMatthew Dillon 		anylink = 1;
516ff1c9800SMatthew Dillon 
517ff1c9800SMatthew Dillon 	if ((dir = opendir(snapshots_path)) != NULL) {
518ff1c9800SMatthew Dillon 		while ((den = readdir(dir)) != NULL) {
519ff1c9800SMatthew Dillon 			if (den->d_name[0] == '.')
520ff1c9800SMatthew Dillon 				continue;
521ff1c9800SMatthew Dillon 			asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
522ff1c9800SMatthew Dillon 			if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode) &&
5235e435c92SMatthew Dillon 			    (anylink || strncmp(den->d_name, "snap-", 5) == 0)
5245e435c92SMatthew Dillon 			) {
525ff1c9800SMatthew Dillon 				if (check_expired(den->d_name, arg2)) {
526ff1c9800SMatthew Dillon 					if (VerboseOpt) {
527ff1c9800SMatthew Dillon 						printf("    expire %s\n",
528ff1c9800SMatthew Dillon 							fpath);
529ff1c9800SMatthew Dillon 					}
530ff1c9800SMatthew Dillon 					remove(fpath);
531ff1c9800SMatthew Dillon 				}
532ff1c9800SMatthew Dillon 			}
533ff1c9800SMatthew Dillon 			free(fpath);
534ff1c9800SMatthew Dillon 		}
535ff1c9800SMatthew Dillon 		closedir(dir);
536ff1c9800SMatthew Dillon 	}
537ff1c9800SMatthew Dillon }
538ff1c9800SMatthew Dillon 
539ff1c9800SMatthew Dillon /*
540ff1c9800SMatthew Dillon  * Take a softlink path in the form snap-yyyymmdd-hhmm and the
541ff1c9800SMatthew Dillon  * expiration in seconds (arg2) and return non-zero if the softlink
542ff1c9800SMatthew Dillon  * has expired.
543ff1c9800SMatthew Dillon  */
544ff1c9800SMatthew Dillon static int
545ff1c9800SMatthew Dillon check_expired(const char *fpath, int arg2)
546ff1c9800SMatthew Dillon {
547ff1c9800SMatthew Dillon 	struct tm tm;
548ff1c9800SMatthew Dillon 	time_t t;
549ff1c9800SMatthew Dillon 	int year;
550ff1c9800SMatthew Dillon 	int month;
5515e435c92SMatthew Dillon 	int day = 0;
5525e435c92SMatthew Dillon 	int hour = 0;
5535e435c92SMatthew Dillon 	int minute = 0;
554ff1c9800SMatthew Dillon 	int r;
555ff1c9800SMatthew Dillon 
5565e435c92SMatthew Dillon 	while (*fpath && *fpath != '-' && *fpath != '.')
5575e435c92SMatthew Dillon 		++fpath;
5585e435c92SMatthew Dillon 	if (*fpath)
5595e435c92SMatthew Dillon 		++fpath;
5605e435c92SMatthew Dillon 
5615e435c92SMatthew Dillon 	r = sscanf(fpath, "%4d%2d%2d-%2d%2d",
562ff1c9800SMatthew Dillon 		   &year, &month, &day, &hour, &minute);
5635e435c92SMatthew Dillon 
5645e435c92SMatthew Dillon 	if (r >= 3) {
565ff1c9800SMatthew Dillon 		bzero(&tm, sizeof(tm));
566ff1c9800SMatthew Dillon 		tm.tm_isdst = -1;
567ff1c9800SMatthew Dillon 		tm.tm_min = minute;
568ff1c9800SMatthew Dillon 		tm.tm_hour = hour;
569ff1c9800SMatthew Dillon 		tm.tm_mday = day;
570ff1c9800SMatthew Dillon 		tm.tm_mon = month - 1;
571ff1c9800SMatthew Dillon 		tm.tm_year = year - 1900;
5725e435c92SMatthew Dillon 		t = mktime(&tm);
5735e435c92SMatthew Dillon 		if (t == (time_t)-1)
5745e435c92SMatthew Dillon 			return(0);
5755e435c92SMatthew Dillon 		t = time(NULL) - t;
576ff1c9800SMatthew Dillon 		if ((int)t > arg2)
577ff1c9800SMatthew Dillon 			return(1);
578ff1c9800SMatthew Dillon 	}
579ff1c9800SMatthew Dillon 	return(0);
580ff1c9800SMatthew Dillon }
581ff1c9800SMatthew Dillon 
582ff1c9800SMatthew Dillon /*
5836a6e350fSMatthew Dillon  * Issue a snapshot.
5846a6e350fSMatthew Dillon  */
5856a6e350fSMatthew Dillon static int
5866a6e350fSMatthew Dillon cleanup_snapshots(const char *path __unused, const char *snapshots_path,
5876a6e350fSMatthew Dillon 		  int arg1 __unused, int arg2 __unused)
5886a6e350fSMatthew Dillon {
5896a6e350fSMatthew Dillon 	int r;
5906a6e350fSMatthew Dillon 
591ff1c9800SMatthew Dillon 	runcmd(&r, "hammer snapshot %s %s", path, snapshots_path);
5926a6e350fSMatthew Dillon 	return(r);
5936a6e350fSMatthew Dillon }
5946a6e350fSMatthew Dillon 
5956a6e350fSMatthew Dillon static int
5966a6e350fSMatthew Dillon cleanup_prune(const char *path __unused, const char *snapshots_path,
597c6c298a7SMatthew Dillon 		  int arg1 __unused, int arg2, int snapshots_disabled)
5986a6e350fSMatthew Dillon {
599c6c298a7SMatthew Dillon 	/*
600c6c298a7SMatthew Dillon 	 * If snapshots have been disabled run prune-everything instead
601c6c298a7SMatthew Dillon 	 * of prune.
602c6c298a7SMatthew Dillon 	 */
603c6c298a7SMatthew Dillon 	if (snapshots_disabled && arg2) {
604c6c298a7SMatthew Dillon 		runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune-everything %s",
605c6c298a7SMatthew Dillon 			snapshots_path, arg2, path);
606c6c298a7SMatthew Dillon 	} else if (snapshots_disabled) {
607c6c298a7SMatthew Dillon 		runcmd(NULL, "hammer prune-everything %s", path);
608c6c298a7SMatthew Dillon 	} else if (arg2) {
6096a6e350fSMatthew Dillon 		runcmd(NULL, "hammer -c %s/.prune.cycle -t %d prune %s",
6106a6e350fSMatthew Dillon 			snapshots_path, arg2, snapshots_path);
6116a6e350fSMatthew Dillon 	} else {
6126a6e350fSMatthew Dillon 		runcmd(NULL, "hammer prune %s", snapshots_path);
6136a6e350fSMatthew Dillon 	}
6146a6e350fSMatthew Dillon 	return(0);
6156a6e350fSMatthew Dillon }
6166a6e350fSMatthew Dillon 
6176a6e350fSMatthew Dillon static int
6186a6e350fSMatthew Dillon cleanup_reblock(const char *path, const char *snapshots_path,
6196a6e350fSMatthew Dillon 		  int arg1 __unused, int arg2)
6206a6e350fSMatthew Dillon {
6216a6e350fSMatthew Dillon 	if (VerboseOpt == 0) {
6226a6e350fSMatthew Dillon 		printf(".");
6236a6e350fSMatthew Dillon 		fflush(stdout);
6246a6e350fSMatthew Dillon 	}
6256a6e350fSMatthew Dillon 	runcmd(NULL,
6266a6e350fSMatthew Dillon 	       "hammer -c %s/.reblock-1.cycle -t %d reblock-btree %s 95",
6276a6e350fSMatthew Dillon 	       snapshots_path, arg2, path);
6286a6e350fSMatthew Dillon 	if (VerboseOpt == 0) {
6296a6e350fSMatthew Dillon 		printf(".");
6306a6e350fSMatthew Dillon 		fflush(stdout);
6316a6e350fSMatthew Dillon 	}
6326a6e350fSMatthew Dillon 	runcmd(NULL,
6336a6e350fSMatthew Dillon 	       "hammer -c %s/.reblock-2.cycle -t %d reblock-inodes %s 95",
6346a6e350fSMatthew Dillon 	       snapshots_path, arg2, path);
6356a6e350fSMatthew Dillon 	if (VerboseOpt == 0) {
6366a6e350fSMatthew Dillon 		printf(".");
6376a6e350fSMatthew Dillon 		fflush(stdout);
6386a6e350fSMatthew Dillon 	}
6396a6e350fSMatthew Dillon 	runcmd(NULL,
6406a6e350fSMatthew Dillon 	       "hammer -c %s/.reblock-3.cycle -t %d reblock-data %s 95",
6416a6e350fSMatthew Dillon 	       snapshots_path, arg2, path);
6426a6e350fSMatthew Dillon 	if (VerboseOpt == 0)
6436a6e350fSMatthew Dillon 		printf("\n");
6446a6e350fSMatthew Dillon 	return(0);
6456a6e350fSMatthew Dillon }
6466a6e350fSMatthew Dillon 
6476a6e350fSMatthew Dillon static int
6486a6e350fSMatthew Dillon cleanup_recopy(const char *path, const char *snapshots_path,
6496a6e350fSMatthew Dillon 		  int arg1 __unused, int arg2)
6506a6e350fSMatthew Dillon {
6516a6e350fSMatthew Dillon 	if (VerboseOpt == 0) {
6526a6e350fSMatthew Dillon 		printf(".");
6536a6e350fSMatthew Dillon 		fflush(stdout);
6546a6e350fSMatthew Dillon 	}
6556a6e350fSMatthew Dillon 	runcmd(NULL,
6566a6e350fSMatthew Dillon 	       "hammer -c %s/.recopy-1.cycle -t %d reblock-btree %s",
6576a6e350fSMatthew Dillon 	       snapshots_path, arg2, path);
6586a6e350fSMatthew Dillon 	if (VerboseOpt == 0) {
6596a6e350fSMatthew Dillon 		printf(".");
6606a6e350fSMatthew Dillon 		fflush(stdout);
6616a6e350fSMatthew Dillon 	}
6626a6e350fSMatthew Dillon 	runcmd(NULL,
6636a6e350fSMatthew Dillon 	       "hammer -c %s/.recopy-2.cycle -t %d reblock-inodes %s",
6646a6e350fSMatthew Dillon 	       snapshots_path, arg2, path);
6656a6e350fSMatthew Dillon 	if (VerboseOpt == 0) {
6666a6e350fSMatthew Dillon 		printf(".");
6676a6e350fSMatthew Dillon 		fflush(stdout);
6686a6e350fSMatthew Dillon 	}
6696a6e350fSMatthew Dillon 	runcmd(NULL,
6706a6e350fSMatthew Dillon 	       "hammer -c %s/.recopy-3.cycle -t %d reblock-data %s",
6716a6e350fSMatthew Dillon 	       snapshots_path, arg2, path);
6726a6e350fSMatthew Dillon 	if (VerboseOpt == 0)
6736a6e350fSMatthew Dillon 		printf("\n");
6746a6e350fSMatthew Dillon 	return(0);
6756a6e350fSMatthew Dillon }
6766a6e350fSMatthew Dillon 
6776a6e350fSMatthew Dillon static
6786a6e350fSMatthew Dillon void
6796a6e350fSMatthew Dillon runcmd(int *resp, const char *ctl, ...)
6806a6e350fSMatthew Dillon {
6816a6e350fSMatthew Dillon 	va_list va;
6826a6e350fSMatthew Dillon 	char *cmd;
6836a6e350fSMatthew Dillon 	char *arg;
6846a6e350fSMatthew Dillon 	char **av;
6856a6e350fSMatthew Dillon 	int n;
6866a6e350fSMatthew Dillon 	int nmax;
6876a6e350fSMatthew Dillon 	int res;
6886a6e350fSMatthew Dillon 	pid_t pid;
6896a6e350fSMatthew Dillon 
6906a6e350fSMatthew Dillon 	/*
6916a6e350fSMatthew Dillon 	 * Generate the command
6926a6e350fSMatthew Dillon 	 */
6936a6e350fSMatthew Dillon 	va_start(va, ctl);
6946a6e350fSMatthew Dillon 	vasprintf(&cmd, ctl, va);
6956a6e350fSMatthew Dillon 	va_end(va);
6966a6e350fSMatthew Dillon 	if (VerboseOpt)
6976a6e350fSMatthew Dillon 		printf("    %s\n", cmd);
6986a6e350fSMatthew Dillon 
6996a6e350fSMatthew Dillon 	/*
7006a6e350fSMatthew Dillon 	 * Break us down into arguments.  We do not just use system() here
7016a6e350fSMatthew Dillon 	 * because it blocks SIGINT and friends.
7026a6e350fSMatthew Dillon 	 */
7036a6e350fSMatthew Dillon 	n = 0;
7046a6e350fSMatthew Dillon 	nmax = 16;
7056a6e350fSMatthew Dillon 	av = malloc(sizeof(char *) * nmax);
7066a6e350fSMatthew Dillon 
7076a6e350fSMatthew Dillon 	for (arg = strtok(cmd, WS); arg; arg = strtok(NULL, WS)) {
708c453712aSMatthew Dillon 		if (n == nmax - 1) {
7096a6e350fSMatthew Dillon 			nmax += 16;
7106a6e350fSMatthew Dillon 			av = realloc(av, sizeof(char *) * nmax);
7116a6e350fSMatthew Dillon 		}
7126a6e350fSMatthew Dillon 		av[n++] = arg;
7136a6e350fSMatthew Dillon 	}
714c453712aSMatthew Dillon 	av[n++] = NULL;
7156a6e350fSMatthew Dillon 
7166a6e350fSMatthew Dillon 	/*
7176a6e350fSMatthew Dillon 	 * Run the command.
7186a6e350fSMatthew Dillon 	 */
719445faa69SMatthew Dillon 	RunningIoctl = 1;
7206a6e350fSMatthew Dillon 	if ((pid = fork()) == 0) {
7216a6e350fSMatthew Dillon 		if (VerboseOpt < 2) {
7226a6e350fSMatthew Dillon 			int fd = open("/dev/null", O_RDWR);
7236a6e350fSMatthew Dillon 			dup2(fd, 1);
7246a6e350fSMatthew Dillon 			close(fd);
7256a6e350fSMatthew Dillon 		}
7266a6e350fSMatthew Dillon 		execvp(av[0], av);
7276a6e350fSMatthew Dillon 		_exit(127);
7286a6e350fSMatthew Dillon 	} else if (pid < 0) {
7296a6e350fSMatthew Dillon 		res = 127;
7306a6e350fSMatthew Dillon 	} else {
7316a6e350fSMatthew Dillon 		int status;
732445faa69SMatthew Dillon 
7336a6e350fSMatthew Dillon 		while (waitpid(pid, &status, 0) != pid)
7346a6e350fSMatthew Dillon 			;
7356a6e350fSMatthew Dillon 		res = WEXITSTATUS(status);
7366a6e350fSMatthew Dillon 	}
737445faa69SMatthew Dillon 	RunningIoctl = 0;
738445faa69SMatthew Dillon 	if (DidInterrupt)
739445faa69SMatthew Dillon 		_exit(1);
7406a6e350fSMatthew Dillon 
7416a6e350fSMatthew Dillon 	free(cmd);
7426a6e350fSMatthew Dillon 	free(av);
7436a6e350fSMatthew Dillon 	if (resp)
7446a6e350fSMatthew Dillon 		*resp = res;
7456a6e350fSMatthew Dillon }
7466a6e350fSMatthew Dillon 
7476a6e350fSMatthew Dillon 
748