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 25dced9b 2019-09-15 hiltjo if (len >= sizeof(wkl->wkl_path)) {
55 25dced9b 2019-09-15 hiltjo free(wkl);
56 f54c4c24 2019-02-08 stsp return got_error(GOT_ERR_NO_SPACE);
57 25dced9b 2019-09-15 hiltjo }
58 89fd757a 2019-02-07 stsp
59 89fd757a 2019-02-07 stsp sigfillset(&new);
60 89fd757a 2019-02-07 stsp sigprocmask(SIG_BLOCK, &new, &old);
61 89fd757a 2019-02-07 stsp SLIST_INSERT_HEAD(worklist, wkl, wkl_list);
62 89fd757a 2019-02-07 stsp sigprocmask(SIG_SETMASK, &old, NULL);
63 f54c4c24 2019-02-08 stsp return NULL;
64 89fd757a 2019-02-07 stsp }
65 89fd757a 2019-02-07 stsp
66 89fd757a 2019-02-07 stsp /*
67 89fd757a 2019-02-07 stsp * run over the given worklist, calling cb for each element.
68 89fd757a 2019-02-07 stsp * this is just like worklist_clean(), except we block signals first.
69 89fd757a 2019-02-07 stsp */
70 89fd757a 2019-02-07 stsp void
71 89fd757a 2019-02-07 stsp worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
72 89fd757a 2019-02-07 stsp {
73 89fd757a 2019-02-07 stsp sigset_t old, new;
74 89fd757a 2019-02-07 stsp struct worklist *wkl;
75 89fd757a 2019-02-07 stsp
76 89fd757a 2019-02-07 stsp sigfillset(&new);
77 89fd757a 2019-02-07 stsp sigprocmask(SIG_BLOCK, &new, &old);
78 89fd757a 2019-02-07 stsp
79 89fd757a 2019-02-07 stsp worklist_clean(list, cb);
80 89fd757a 2019-02-07 stsp
81 89fd757a 2019-02-07 stsp while ((wkl = SLIST_FIRST(list)) != NULL) {
82 89fd757a 2019-02-07 stsp SLIST_REMOVE_HEAD(list, wkl_list);
83 89fd757a 2019-02-07 stsp free(wkl);
84 89fd757a 2019-02-07 stsp }
85 89fd757a 2019-02-07 stsp
86 89fd757a 2019-02-07 stsp sigprocmask(SIG_SETMASK, &old, NULL);
87 89fd757a 2019-02-07 stsp }
88 89fd757a 2019-02-07 stsp
89 89fd757a 2019-02-07 stsp /*
90 89fd757a 2019-02-07 stsp * pass elements to the specified callback, which has to be signal safe.
91 89fd757a 2019-02-07 stsp */
92 89fd757a 2019-02-07 stsp void
93 89fd757a 2019-02-07 stsp worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
94 89fd757a 2019-02-07 stsp {
95 89fd757a 2019-02-07 stsp struct worklist *wkl;
96 89fd757a 2019-02-07 stsp
97 89fd757a 2019-02-07 stsp SLIST_FOREACH(wkl, list, wkl_list)
98 89fd757a 2019-02-07 stsp cb(wkl);
99 89fd757a 2019-02-07 stsp }
100 89fd757a 2019-02-07 stsp
101 89fd757a 2019-02-07 stsp void
102 89fd757a 2019-02-07 stsp worklist_unlink(struct worklist *wkl)
103 89fd757a 2019-02-07 stsp {
104 89fd757a 2019-02-07 stsp (void)unlink(wkl->wkl_path);
105 89fd757a 2019-02-07 stsp }