]> diplodocus.org Git - nmh/blob - sbr/context_save.c
sendsbr.c: Move interface to own file.
[nmh] / sbr / context_save.c
1 /* context_save.c -- write out the updated context file
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 /*
9 * This function used to support setuid/setgid programs by writing
10 * the file as the user. But that code, m_chkids(), was removed
11 * because there no longer are setuid/setgid programs in nmh.
12 */
13
14 #include "h/mh.h"
15 #include "context_save.h"
16 #include "error.h"
17 #include "h/signals.h"
18 #include "lock_file.h"
19
20 void
21 context_save (void)
22 {
23 struct node *np;
24 FILE *out;
25 sigset_t set, oset;
26 int failed_to_lock = 0;
27
28 /* No context in use -- silently ignore any changes! */
29 if (!ctxpath)
30 return;
31
32 if (!(ctxflags & CTXMOD))
33 return;
34 ctxflags &= ~CTXMOD;
35
36 /* block a few signals */
37 sigemptyset (&set);
38 sigaddset (&set, SIGHUP);
39 sigaddset (&set, SIGINT);
40 sigaddset (&set, SIGQUIT);
41 sigaddset (&set, SIGTERM);
42 sigprocmask (SIG_BLOCK, &set, &oset);
43
44 if (!(out = lkfopendata (ctxpath, "w", &failed_to_lock))) {
45 if (failed_to_lock)
46 adios (ctxpath, "failed to lock");
47 adios (ctxpath, "unable to write");
48 }
49 for (np = m_defs; np; np = np->n_next)
50 if (np->n_context)
51 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
52 lkfclosedata (out, ctxpath);
53
54 sigprocmask (SIG_SETMASK, &oset, &set); /* reset the signal mask */
55 }