xref: /dflybsd-src/sbin/hammer/cmd_abort.c (revision 52e2f1b52011af14814bda6122c29e91ab740ce3)
1a360fddeSJohn Marino /*
2a360fddeSJohn Marino  * Copyright (c) 2015 The DragonFly Project.  All rights reserved.
3a360fddeSJohn Marino  *
4a360fddeSJohn Marino  * This code is derived from software contributed to The DragonFly Project
5a360fddeSJohn Marino  * by John Marino <draco@marino.st>
6a360fddeSJohn Marino  *
7a360fddeSJohn Marino  * Redistribution and use in source and binary forms, with or without
8a360fddeSJohn Marino  * modification, are permitted provided that the following conditions
9a360fddeSJohn Marino  * are met:
10a360fddeSJohn Marino  *
11a360fddeSJohn Marino  * 1. Redistributions of source code must retain the above copyright
12a360fddeSJohn Marino  *    notice, this list of conditions and the following disclaimer.
13a360fddeSJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
14a360fddeSJohn Marino  *    notice, this list of conditions and the following disclaimer in
15a360fddeSJohn Marino  *    the documentation and/or other materials provided with the
16a360fddeSJohn Marino  *    distribution.
17a360fddeSJohn Marino  * 3. Neither the name of The DragonFly Project nor the names of its
18a360fddeSJohn Marino  *    contributors may be used to endorse or promote products derived
19a360fddeSJohn Marino  *    from this software without specific, prior written permission.
20a360fddeSJohn Marino  *
21a360fddeSJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22a360fddeSJohn Marino  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23a360fddeSJohn Marino  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24a360fddeSJohn Marino  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25a360fddeSJohn Marino  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26a360fddeSJohn Marino  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27a360fddeSJohn Marino  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28a360fddeSJohn Marino  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29a360fddeSJohn Marino  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30a360fddeSJohn Marino  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31a360fddeSJohn Marino  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32a360fddeSJohn Marino  * SUCH DAMAGE.
33a360fddeSJohn Marino  */
34a360fddeSJohn Marino /*
35a360fddeSJohn Marino  * Each "hammer cleanup" command creates a pid file at /var/run with the
36a360fddeSJohn Marino  * name hammer.cleanup.$pid with the contents of $pid
37a360fddeSJohn Marino  *
38a360fddeSJohn Marino  * If the cleanup ends without incident, the pid file is removed.  If the
39a360fddeSJohn Marino  * cleanup job is interrupted, the pid file is not removed.  This is
40a360fddeSJohn Marino  * because SIGINT is disabled in deferrence to HAMMER ioctl.
41a360fddeSJohn Marino  *
42a360fddeSJohn Marino  * The "hammer abort-cleanup" command is simple.  It scans /var/run for
43a360fddeSJohn Marino  * all files starting with "hammer.cleanup.", reads them, and issues a
44a360fddeSJohn Marino  * SIGINTR for any valid pid.  Every hammer.cleanup.XXXXXX file will be
45a360fddeSJohn Marino  * removed after the command executes.  If multiple cleanup jobs are
46a360fddeSJohn Marino  * running simultaneously, all of them will be aborted.
47a360fddeSJohn Marino  *
48a360fddeSJohn Marino  * It is intended any future "abort" commands are also placed in here.
49a360fddeSJohn Marino  */
50a360fddeSJohn Marino 
51a360fddeSJohn Marino #include "hammer.h"
52a360fddeSJohn Marino 
53a360fddeSJohn Marino void
hammer_cmd_abort_cleanup(char ** av __unused,int ac __unused)54a360fddeSJohn Marino hammer_cmd_abort_cleanup(char **av __unused, int ac __unused)
55a360fddeSJohn Marino {
56a360fddeSJohn Marino 	DIR *dir;
57a360fddeSJohn Marino 	pid_t pid;
58a360fddeSJohn Marino 	char *str;
59a360fddeSJohn Marino 	int pf_fd;
60a360fddeSJohn Marino 	struct dirent *den;
61a360fddeSJohn Marino 	static char pidfile[PIDFILE_BUFSIZE];
62a360fddeSJohn Marino 	static const char prefix[] = "hammer.cleanup.";
63a360fddeSJohn Marino 	static const char termmsg[] = "Terminated cleanup process %u\n";
64a360fddeSJohn Marino 	const size_t pflen = sizeof(prefix) - 1;
65a360fddeSJohn Marino 
66*52e2f1b5STomohiro Kusumi 	if ((dir = opendir(pidfile_loc)) == NULL)
67a360fddeSJohn Marino 	        return;
68a360fddeSJohn Marino 
69a360fddeSJohn Marino 	while ((den = readdir(dir)) != NULL) {
70a360fddeSJohn Marino 		if (strncmp(den->d_name, prefix, pflen) == 0) {
71a360fddeSJohn Marino 			snprintf (pidfile, PIDFILE_BUFSIZE, "%s/%s",
72a360fddeSJohn Marino 				pidfile_loc, den->d_name);
73a360fddeSJohn Marino 			pid = strtol((char *)(den->d_name + pflen), &str, 10);
74a360fddeSJohn Marino 			pf_fd = open(pidfile, O_RDONLY | O_CLOEXEC);
75*52e2f1b5STomohiro Kusumi 			if (pf_fd == -1)
76a360fddeSJohn Marino 				continue;
77a360fddeSJohn Marino 
78a360fddeSJohn Marino 			if (flock(pf_fd, LOCK_EX | LOCK_NB) < 0) {
79a360fddeSJohn Marino 				if (errno == EWOULDBLOCK) {
80a360fddeSJohn Marino 					/* error expected during cleanup */
81*52e2f1b5STomohiro Kusumi 					if (kill (pid, SIGTERM) == 0)
82a360fddeSJohn Marino 						printf (termmsg, pid);
83a360fddeSJohn Marino 				}
84*52e2f1b5STomohiro Kusumi 			} else {
85a360fddeSJohn Marino 				/* lock succeeded so pidfile is stale */
86a360fddeSJohn Marino 				flock (pf_fd, LOCK_UN);
87a360fddeSJohn Marino 			}
88a360fddeSJohn Marino 			close (pf_fd);
89a360fddeSJohn Marino 			unlink (pidfile);
90a360fddeSJohn Marino 		}
91a360fddeSJohn Marino 	}
92a360fddeSJohn Marino 	closedir(dir);
93a360fddeSJohn Marino }
94