xref: /openbsd/usr.bin/rcs/worklist.c (revision 404b540a)
1 /*	$OpenBSD: worklist.c,v 1.2 2007/02/27 07:59:13 xsa Exp $	*/
2 /*
3  * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/queue.h>
28 
29 #include <err.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "worklist.h"
35 #include "xmalloc.h"
36 
37 /*
38  * adds a path to a worklist.
39  */
40 void
41 rcs_worklist_add(const char *path, struct rcs_wklhead *worklist)
42 {
43 	size_t len;
44 	struct rcs_worklist *wkl;
45 	sigset_t old, new;
46 
47 	wkl = xcalloc(1, sizeof(*wkl));
48 
49 	len = strlcpy(wkl->wkl_path, path, sizeof(wkl->wkl_path));
50 	if (len >= sizeof(wkl->wkl_path))
51 		errx(1, "path truncation in rcs_worklist_add");
52 
53 	sigfillset(&new);
54 	sigprocmask(SIG_BLOCK, &new, &old);
55 	SLIST_INSERT_HEAD(worklist, wkl, wkl_list);
56 	sigprocmask(SIG_SETMASK, &old, NULL);
57 }
58 
59 /*
60  * run over the given worklist, calling cb for each element.
61  * this is just like rcs_worklist_clean(), except we block signals first.
62  */
63 void
64 rcs_worklist_run(struct rcs_wklhead *list, void (*cb)(struct rcs_worklist *))
65 {
66 	sigset_t old, new;
67 	struct rcs_worklist *wkl;
68 
69 	sigfillset(&new);
70 	sigprocmask(SIG_BLOCK, &new, &old);
71 
72 	rcs_worklist_clean(list, cb);
73 
74 	while ((wkl = SLIST_FIRST(list)) != NULL) {
75 		SLIST_REMOVE_HEAD(list, wkl_list);
76 		xfree(wkl);
77 	}
78 
79 	sigprocmask(SIG_SETMASK, &old, NULL);
80 }
81 
82 /*
83  * pass elements to the specified callback, which has to be signal safe.
84  */
85 void
86 rcs_worklist_clean(struct rcs_wklhead *list, void (*cb)(struct rcs_worklist *))
87 {
88 	struct rcs_worklist *wkl;
89 
90 	SLIST_FOREACH(wkl, list, wkl_list)
91 	    cb(wkl);
92 }
93 
94 void
95 rcs_worklist_unlink(struct rcs_worklist *wkl)
96 {
97 	(void)unlink(wkl->wkl_path);
98 }
99