Blame


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