2 * Copyright (c) 2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by John Marino <draco@marino.st>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 * Each "hammer cleanup" command creates a pid file at /var/run with the
36 * name hammer.cleanup.$pid with the contents of $pid
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.
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.
48 * It is intended any future "abort" commands are also placed in here.
54 hammer_cmd_abort_cleanup(char **av __unused
, int ac __unused
)
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;
66 if ((dir
= opendir(pidfile_loc
)) == NULL
) {
70 while ((den
= readdir(dir
)) != NULL
) {
71 if (strncmp(den
->d_name
, prefix
, pflen
) == 0) {
72 snprintf (pidfile
, PIDFILE_BUFSIZE
, "%s/%s",
73 pidfile_loc
, den
->d_name
);
74 pid
= strtol((char *)(den
->d_name
+ pflen
), &str
, 10);
75 pf_fd
= open(pidfile
, O_RDONLY
| O_CLOEXEC
);
80 if (flock(pf_fd
, LOCK_EX
| LOCK_NB
) < 0) {
81 if (errno
== EWOULDBLOCK
) {
82 /* error expected during cleanup */
83 if (kill (pid
, SIGTERM
) == 0) {
84 printf (termmsg
, pid
);
89 /* lock succeeded so pidfile is stale */
90 flock (pf_fd
, LOCK_UN
);