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