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 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>
36 89fd757a 2019-02-07 stsp #include "worklist.h"
37 f54c4c24 2019-02-08 stsp #include "got_error.h"
40 89fd757a 2019-02-07 stsp * adds a path to a worklist.
42 f54c4c24 2019-02-08 stsp const struct got_error *
43 89fd757a 2019-02-07 stsp worklist_add(const char *path, struct wklhead *worklist)
46 89fd757a 2019-02-07 stsp struct worklist *wkl;
47 89fd757a 2019-02-07 stsp sigset_t old, new;
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");
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);
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;
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.
69 89fd757a 2019-02-07 stsp worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
71 89fd757a 2019-02-07 stsp sigset_t old, new;
72 89fd757a 2019-02-07 stsp struct worklist *wkl;
74 89fd757a 2019-02-07 stsp sigfillset(&new);
75 89fd757a 2019-02-07 stsp sigprocmask(SIG_BLOCK, &new, &old);
77 89fd757a 2019-02-07 stsp worklist_clean(list, cb);
79 89fd757a 2019-02-07 stsp while ((wkl = SLIST_FIRST(list)) != NULL) {
80 89fd757a 2019-02-07 stsp SLIST_REMOVE_HEAD(list, wkl_list);
84 89fd757a 2019-02-07 stsp sigprocmask(SIG_SETMASK, &old, NULL);
88 89fd757a 2019-02-07 stsp * pass elements to the specified callback, which has to be signal safe.
91 89fd757a 2019-02-07 stsp worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
93 89fd757a 2019-02-07 stsp struct worklist *wkl;
95 89fd757a 2019-02-07 stsp SLIST_FOREACH(wkl, list, wkl_list)
100 89fd757a 2019-02-07 stsp worklist_unlink(struct worklist *wkl)
102 89fd757a 2019-02-07 stsp (void)unlink(wkl->wkl_path);