]> git.notmuchmail.org Git - notmuch/blob - hooks.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / hooks.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * This file is part of notmuch.
4  *
5  * Copyright © 2011 Jani Nikula
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see https://www.gnu.org/licenses/ .
19  *
20  * Author: Jani Nikula <jani@nikula.org>
21  */
22
23 #include "notmuch-client.h"
24 #include <sys/wait.h>
25
26 int
27 notmuch_run_hook (notmuch_database_t *notmuch, const char *hook)
28 {
29     char *hook_path;
30     const char *config_path;
31     int status = 0;
32     pid_t pid;
33
34     hook_path = talloc_asprintf (notmuch, "%s/%s",
35                                  notmuch_config_get (notmuch, NOTMUCH_CONFIG_HOOK_DIR),
36                                  hook);
37     if (hook_path == NULL) {
38         fprintf (stderr, "Out of memory\n");
39         return 1;
40     }
41
42     config_path = notmuch_config_path (notmuch);
43     if (setenv ("NOTMUCH_CONFIG", config_path, 1)) {
44         perror ("setenv");
45         return 1;
46     }
47
48     /* Check access before fork() for speed and simplicity of error handling. */
49     if (access (hook_path, X_OK) == -1) {
50         /* Ignore ENOENT. It's okay not to have a hook, hook dir, or even
51          * notmuch dir. Dangling symbolic links also result in ENOENT, but
52          * we'll ignore that too for simplicity. */
53         if (errno != ENOENT) {
54             fprintf (stderr, "Error: %s hook access failed: %s\n", hook,
55                      strerror (errno));
56             status = 1;
57         }
58         goto DONE;
59     }
60
61     /* Flush any buffered output before forking. */
62     fflush (stdout);
63
64     pid = fork ();
65     if (pid == -1) {
66         fprintf (stderr, "Error: %s hook fork failed: %s\n", hook,
67                  strerror (errno));
68         status = 1;
69         goto DONE;
70     } else if (pid == 0) {
71         execl (hook_path, hook_path, NULL);
72         /* Same as above for ENOENT, but unlikely now. Indicate all other errors
73          * to parent through non-zero exit status. */
74         if (errno != ENOENT) {
75             fprintf (stderr, "Error: %s hook execution failed: %s\n", hook,
76                      strerror (errno));
77             status = 1;
78         }
79         exit (status);
80     }
81
82     if (waitpid (pid, &status, 0) == -1) {
83         fprintf (stderr, "Error: %s hook wait failed: %s\n", hook,
84                  strerror (errno));
85         status = 1;
86         goto DONE;
87     }
88
89     if (! WIFEXITED (status) || WEXITSTATUS (status)) {
90         if (WIFEXITED (status)) {
91             fprintf (stderr, "Error: %s hook failed with status %d\n",
92                      hook, WEXITSTATUS (status));
93         } else if (WIFSIGNALED (status)) {
94             fprintf (stderr, "Error: %s hook terminated with signal %d\n",
95                      hook, WTERMSIG (status));
96         }
97         status = 1;
98     }
99
100   DONE:
101     talloc_free (hook_path);
102
103     return status;
104 }