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