1 89fd757a 2019-02-07 stsp /* $OpenBSD: worklist.c,v 1.4 2015/06/13 20:15:21 nicm Exp $ */
3 89fd757a 2019-02-07 stsp * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4 89fd757a 2019-02-07 stsp * All rights reserved.
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
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.
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.
27 89fd757a 2019-02-07 stsp #include <sys/queue.h>
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>
35 89fd757a 2019-02-07 stsp #include "worklist.h"
36 89fd757a 2019-02-07 stsp #include "xmalloc.h"
39 89fd757a 2019-02-07 stsp * adds a path to a worklist.
42 89fd757a 2019-02-07 stsp worklist_add(const char *path, struct wklhead *worklist)
45 89fd757a 2019-02-07 stsp struct worklist *wkl;
46 89fd757a 2019-02-07 stsp sigset_t old, new;
48 89fd757a 2019-02-07 stsp wkl = xcalloc(1, sizeof(*wkl));
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");
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);
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.
65 89fd757a 2019-02-07 stsp worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
67 89fd757a 2019-02-07 stsp sigset_t old, new;
68 89fd757a 2019-02-07 stsp struct worklist *wkl;
70 89fd757a 2019-02-07 stsp sigfillset(&new);
71 89fd757a 2019-02-07 stsp sigprocmask(SIG_BLOCK, &new, &old);
73 89fd757a 2019-02-07 stsp worklist_clean(list, cb);
75 89fd757a 2019-02-07 stsp while ((wkl = SLIST_FIRST(list)) != NULL) {
76 89fd757a 2019-02-07 stsp SLIST_REMOVE_HEAD(list, wkl_list);
80 89fd757a 2019-02-07 stsp sigprocmask(SIG_SETMASK, &old, NULL);
84 89fd757a 2019-02-07 stsp * pass elements to the specified callback, which has to be signal safe.
87 89fd757a 2019-02-07 stsp worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
89 89fd757a 2019-02-07 stsp struct worklist *wkl;
91 89fd757a 2019-02-07 stsp SLIST_FOREACH(wkl, list, wkl_list)
96 89fd757a 2019-02-07 stsp worklist_unlink(struct worklist *wkl)
98 89fd757a 2019-02-07 stsp (void)unlink(wkl->wkl_path);