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 f54c4c24 2019-02-08 stsp #include <stdio.h>
33 89fd757a 2019-02-07 stsp #include <string.h>
34 89fd757a 2019-02-07 stsp #include <unistd.h>
35 89fd757a 2019-02-07 stsp
36 89fd757a 2019-02-07 stsp #include "worklist.h"
37 f54c4c24 2019-02-08 stsp #include "got_error.h"
38 89fd757a 2019-02-07 stsp
39 89fd757a 2019-02-07 stsp /*
40 89fd757a 2019-02-07 stsp * adds a path to a worklist.
41 89fd757a 2019-02-07 stsp */
42 f54c4c24 2019-02-08 stsp const struct got_error *
43 89fd757a 2019-02-07 stsp worklist_add(const char *path, struct wklhead *worklist)
44 89fd757a 2019-02-07 stsp {
45 89fd757a 2019-02-07 stsp size_t len;
46 89fd757a 2019-02-07 stsp struct worklist *wkl;
47 89fd757a 2019-02-07 stsp sigset_t old, new;
48 89fd757a 2019-02-07 stsp
49 f54c4c24 2019-02-08 stsp wkl = calloc(1, sizeof(*wkl));
50 f54c4c24 2019-02-08 stsp if (wkl == NULL)
51 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
52 89fd757a 2019-02-07 stsp
53 89fd757a 2019-02-07 stsp len = strlcpy(wkl->wkl_path, path, sizeof(wkl->wkl_path));
54 89fd757a 2019-02-07 stsp if (len >= sizeof(wkl->wkl_path))
55 f54c4c24 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
56 89fd757a 2019-02-07 stsp
57 89fd757a 2019-02-07 stsp sigfillset(&new);
58 89fd757a 2019-02-07 stsp sigprocmask(SIG_BLOCK, &new, &old);
59 89fd757a 2019-02-07 stsp SLIST_INSERT_HEAD(worklist, wkl, wkl_list);
60 89fd757a 2019-02-07 stsp sigprocmask(SIG_SETMASK, &old, NULL);
61 f54c4c24 2019-02-08 stsp return NULL;
62 89fd757a 2019-02-07 stsp }
63 89fd757a 2019-02-07 stsp
64 89fd757a 2019-02-07 stsp /*
65 89fd757a 2019-02-07 stsp * run over the given worklist, calling cb for each element.
66 89fd757a 2019-02-07 stsp * this is just like worklist_clean(), except we block signals first.
67 89fd757a 2019-02-07 stsp */
68 89fd757a 2019-02-07 stsp void
69 89fd757a 2019-02-07 stsp worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
70 89fd757a 2019-02-07 stsp {
71 89fd757a 2019-02-07 stsp sigset_t old, new;
72 89fd757a 2019-02-07 stsp struct worklist *wkl;
73 89fd757a 2019-02-07 stsp
74 89fd757a 2019-02-07 stsp sigfillset(&new);
75 89fd757a 2019-02-07 stsp sigprocmask(SIG_BLOCK, &new, &old);
76 89fd757a 2019-02-07 stsp
77 89fd757a 2019-02-07 stsp worklist_clean(list, cb);
78 89fd757a 2019-02-07 stsp
79 89fd757a 2019-02-07 stsp while ((wkl = SLIST_FIRST(list)) != NULL) {
80 89fd757a 2019-02-07 stsp SLIST_REMOVE_HEAD(list, wkl_list);
81 89fd757a 2019-02-07 stsp free(wkl);
82 89fd757a 2019-02-07 stsp }
83 89fd757a 2019-02-07 stsp
84 89fd757a 2019-02-07 stsp sigprocmask(SIG_SETMASK, &old, NULL);
85 89fd757a 2019-02-07 stsp }
86 89fd757a 2019-02-07 stsp
87 89fd757a 2019-02-07 stsp /*
88 89fd757a 2019-02-07 stsp * pass elements to the specified callback, which has to be signal safe.
89 89fd757a 2019-02-07 stsp */
90 89fd757a 2019-02-07 stsp void
91 89fd757a 2019-02-07 stsp worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
92 89fd757a 2019-02-07 stsp {
93 89fd757a 2019-02-07 stsp struct worklist *wkl;
94 89fd757a 2019-02-07 stsp
95 89fd757a 2019-02-07 stsp SLIST_FOREACH(wkl, list, wkl_list)
96 89fd757a 2019-02-07 stsp cb(wkl);
97 89fd757a 2019-02-07 stsp }
98 89fd757a 2019-02-07 stsp
99 89fd757a 2019-02-07 stsp void
100 89fd757a 2019-02-07 stsp worklist_unlink(struct worklist *wkl)
101 89fd757a 2019-02-07 stsp {
102 89fd757a 2019-02-07 stsp (void)unlink(wkl->wkl_path);
103 89fd757a 2019-02-07 stsp }