Blob


1 /* $OpenBSD: worklist.c,v 1.4 2015/06/13 20:15:21 nicm 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 */
27 #include <sys/queue.h>
29 #include <err.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "worklist.h"
37 #include "got_error.h"
39 /*
40 * adds a path to a worklist.
41 */
42 const struct got_error *
43 worklist_add(const char *path, struct wklhead *worklist)
44 {
45 size_t len;
46 struct worklist *wkl;
47 sigset_t old, new;
49 wkl = calloc(1, sizeof(*wkl));
50 if (wkl == NULL)
51 return got_error_from_errno("calloc");
53 len = strlcpy(wkl->wkl_path, path, sizeof(wkl->wkl_path));
54 if (len >= sizeof(wkl->wkl_path)) {
55 free(wkl);
56 return got_error(GOT_ERR_NO_SPACE);
57 }
59 sigfillset(&new);
60 sigprocmask(SIG_BLOCK, &new, &old);
61 SLIST_INSERT_HEAD(worklist, wkl, wkl_list);
62 sigprocmask(SIG_SETMASK, &old, NULL);
63 return NULL;
64 }
66 /*
67 * run over the given worklist, calling cb for each element.
68 * this is just like worklist_clean(), except we block signals first.
69 */
70 void
71 worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
72 {
73 sigset_t old, new;
74 struct worklist *wkl;
76 sigfillset(&new);
77 sigprocmask(SIG_BLOCK, &new, &old);
79 worklist_clean(list, cb);
81 while ((wkl = SLIST_FIRST(list)) != NULL) {
82 SLIST_REMOVE_HEAD(list, wkl_list);
83 free(wkl);
84 }
86 sigprocmask(SIG_SETMASK, &old, NULL);
87 }
89 /*
90 * pass elements to the specified callback, which has to be signal safe.
91 */
92 void
93 worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
94 {
95 struct worklist *wkl;
97 SLIST_FOREACH(wkl, list, wkl_list)
98 cb(wkl);
99 }
101 void
102 worklist_unlink(struct worklist *wkl)
104 (void)unlink(wkl->wkl_path);