]> diplodocus.org Git - nmh/blob - sbr/context_save.c
Cleaned up leaks from calls to content_charset() in mhfixmsg.
[nmh] / sbr / context_save.c
1
2 /*
3 * context_save.c -- write out the updated context file
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12
13 /*
14 * static prototypes
15 */
16 static int m_chkids(void);
17
18
19 void
20 context_save (void)
21 {
22 int action;
23 register 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 if ((action = m_chkids ()) > 0)
37 return; /* child did it for us */
38
39 /* block a few signals */
40 sigemptyset (&set);
41 sigaddset (&set, SIGHUP);
42 sigaddset (&set, SIGINT);
43 sigaddset (&set, SIGQUIT);
44 sigaddset (&set, SIGTERM);
45 sigprocmask (SIG_BLOCK, &set, &oset);
46
47 if (!(out = lkfopendata (ctxpath, "w", &failed_to_lock))) {
48 if (failed_to_lock) {
49 adios (ctxpath, "failed to lock");
50 } else {
51 adios (ctxpath, "unable to write");
52 }
53 }
54 for (np = m_defs; np; np = np->n_next)
55 if (np->n_context)
56 fprintf (out, "%s: %s\n", np->n_name, np->n_field);
57 lkfclosedata (out, ctxpath);
58
59 sigprocmask (SIG_SETMASK, &oset, &set); /* reset the signal mask */
60
61 if (action == 0)
62 /* This must be _exit(), not exit(), because the child didn't
63 call unregister_for_removal() in m_chkids(). */
64 _exit (0); /* we are child, time to die */
65 }
66
67 /*
68 * This hack brought to you so we can handle set[ug]id MH programs.
69 * If we return -1, then no fork is made, we update .mh_profile
70 * normally, and return to the caller normally. If we return 0,
71 * then the child is executing, .mh_profile is modified after
72 * we set our [ug]ids to the norm. If we return > 0, then the
73 * parent is executed and .mh_profile has already be modified.
74 * We can just return to the caller immediately.
75 */
76
77 static int
78 m_chkids (void)
79 {
80 int i;
81 pid_t pid;
82
83 if (getuid () == geteuid ())
84 return (-1);
85
86 for (i = 0; (pid = fork ()) == -1 && i < 5; i++)
87 sleep (5);
88
89 switch (pid) {
90 case -1:
91 break;
92
93 case 0:
94 /* It's not necessary to call unregister_for_removal(0)
95 because the child calls _exit() in context_save(). */
96 break;
97
98 default:
99 pidwait (pid, -1);
100 break;
101 }
102
103 return pid;
104 }