]> git.notmuchmail.org Git - notmuch/blob - hooks.c
config: deprecate/drop crypto.gpg_path under gmime 2.6/3.0
[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 (const char *db_path, const char *hook)
28 {
29     char *hook_path;
30     int status = 0;
31     pid_t pid;
32
33     hook_path = talloc_asprintf (NULL, "%s/%s/%s/%s", db_path, ".notmuch",
34                                  "hooks", hook);
35     if (hook_path == NULL) {
36         fprintf (stderr, "Out of memory\n");
37         return 1;
38     }
39
40     /* Check access before fork() for speed and simplicity of error handling. */
41     if (access (hook_path, X_OK) == -1) {
42         /* Ignore ENOENT. It's okay not to have a hook, hook dir, or even
43          * notmuch dir. Dangling symbolic links also result in ENOENT, but
44          * we'll ignore that too for simplicity. */
45         if (errno != ENOENT) {
46             fprintf (stderr, "Error: %s hook access failed: %s\n", hook,
47                      strerror (errno));
48             status = 1;
49         }
50         goto DONE;
51     }
52
53     /* Flush any buffered output before forking. */
54     fflush (stdout);
55
56     pid = fork();
57     if (pid == -1) {
58         fprintf (stderr, "Error: %s hook fork failed: %s\n", hook,
59                  strerror (errno));
60         status = 1;
61         goto DONE;
62     } else if (pid == 0) {
63         execl (hook_path, hook_path, NULL);
64         /* Same as above for ENOENT, but unlikely now. Indicate all other errors
65          * to parent through non-zero exit status. */
66         if (errno != ENOENT) {
67             fprintf (stderr, "Error: %s hook execution failed: %s\n", hook,
68                      strerror (errno));
69             status = 1;
70         }
71         exit (status);
72     }
73
74     if (waitpid (pid, &status, 0) == -1) {
75         fprintf (stderr, "Error: %s hook wait failed: %s\n", hook,
76                  strerror (errno));
77         status = 1;
78         goto DONE;
79     }
80
81     if (!WIFEXITED (status) || WEXITSTATUS (status)) {
82         if (WIFEXITED (status)) {
83             fprintf (stderr, "Error: %s hook failed with status %d\n",
84                      hook, WEXITSTATUS (status));
85         } else if (WIFSIGNALED (status)) {
86             fprintf (stderr, "Error: %s hook terminated with signal %d\n",
87                      hook, WTERMSIG (status));
88         }
89         status = 1;
90     }
91
92   DONE:
93     talloc_free (hook_path);
94
95     return status;
96 }