child-process.c revision 0c423a9cc14c5872f012be9640cb6e0d66a36717
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2002-2009 Dovecot authors, see the included COPYING file */
65f8fb656051f1059f7b5a2da9c5555adcc30439Timo Sirainenconst char *process_names[PROCESS_TYPE_MAX] = {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen "auth-worker",
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen "ssl-build-param",
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainenstatic child_process_destroy_callback_t *destroy_callbacks[PROCESS_TYPE_MAX];
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainenstruct child_process *child_process_lookup(pid_t pid)
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen return hash_table_lookup(processes, POINTER_CAST(pid));
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenvoid child_process_add(pid_t pid, struct child_process *process)
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen hash_table_insert(processes, POINTER_CAST(pid), process);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen hash_table_remove(processes, POINTER_CAST(pid));
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen /* remove all environment, we don't need them */
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen /* we'll log through master process */
e5fd6dfd0a492e4708d4dbb7971d7fc5d7b8fd85Timo Sirainen !syslog_facility_find(settings_root->defaults->syslog_facility,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen env_put(t_strdup_printf("SYSLOG_FACILITY=%d", facility));
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen if (settings_root != NULL && !settings_root->defaults->version_ignore)
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainenvoid client_process_exec(const char *cmd, const char *title)
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen const char **argv;
420040a5930a2b497e79ff0b5f59ba4b764a5b39Timo Sirainen /* very simple argument splitting. */
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainen argv = t_strsplit(t_strconcat(cmd, " ", title, NULL), " ");
70ead6466f9baa8294e71fc2fba0a4f54f488b5eTimo Sirainenvoid client_process_exec_argv(const char *executable, const char **argv)
ccc895c0358108d2304239063e940b7d75f364abTimo Sirainen const char *p;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* hide the path, it's ugly */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic const char *get_exit_status_message(enum fatal_exit_status status,
b9f564d00b7a115f465ffd6840341c7b8f9bfc8aTimo Sirainen return "Can't open log file";
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen return "Can't write to log file";
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return "Internal logging error";
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return "Out of memory - see auth_process_size setting";
17ad2164c747cedbf81dae1893063e71a3df0356Timo Sirainen return "Out of memory - see login_process_size setting";
2649b237dd4690575e75a30b2bf3b39ebd37b835Timo Sirainen return "Out of memory - see mail_process_size setting";
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen return "Out of memory";
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen return "exec() failed";
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen return "Fatal failure";
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainenlog_coredump(string_t *str, enum process_type process_type, int status)
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen if (signum != SIGABRT && signum != SIGSEGV && signum != SIGBUS)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* let's try to figure out why we didn't get a core dump */
48270badadd82279bfe50ae3d187aea8b0b2b30eTimo Sirainen str_append(str, " (core not dumped - add -D to login_executable)");
14ab4610b6038da6c5d0814fecabc6b74bc81a6bTimo Sirainen if (!settings_root->defaults->mail_drop_priv_before_exec) {
84ed9f8f3d0e5ed47607ef417618e49e4f865557Timo Sirainen str_append(str, " (core not dumped - set mail_drop_priv_before_exec=yes)");
e3796bfd2bc0fd5ba664893d346df9334a5b3af0Timo Sirainen if (*settings_root->defaults->mail_privileged_group != '\0') {
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen str_append(str, " (core not dumped - mail_privileged_group prevented it)");
408e5be344c9131fdebe771718a5bf49f88cc51cTimo Sirainen str_append(str, " (core not dumped - is home dir set?)");
ecdce39e5ef4b62eefa9f5818f17d153fd5d710aTimo Sirainen "no permissions for auth user %s in %s?)",
3852872e6954b7132e637294132005e86b8ebd4aTimo Sirainen str_append(str, " (core not dumped - auth user is not root)");
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainenstatic void sigchld_handler(const siginfo_t *si ATTR_UNUSED,
bd1b2615928a1e8be190cb0405754f0aec8cac2fTimo Sirainen while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* get the type and remove from hash */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* write errors to syslog */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen process_type_name = process_names[process_type];
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen /* kludgy. hide this failure. */
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen /* the error was already logged. */
ca98d6a1bbe73499da758a36bfab2963375c8d06Timo Sirainen "child %s (%s) returned error %d%s",
64b5dcc136d6eb7ad90463e6cba9e16880ab52adTimo Sirainen str_printfa(str, "child %s (%s) killed with signal %d",
f23ede27743c1aa03eacbfc634d6a10de9110c91Timo Sirainen if (process != NULL && process->ip.family != 0) {
f81f4bc282cd1944cec187bae89c0701a416ed2aTimo Sirainen if (destroy_callbacks[process_type] != NULL) {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (pid == -1 && errno != EINTR && errno != ECHILD)
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainenvoid child_process_set_destroy_callback(enum process_type type,
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen processes = hash_table_create(default_pool, default_pool, 128, NULL, NULL);
eb64c3586d854cddd693f0b811d897399076a441Timo Sirainen lib_signals_set_handler(SIGCHLD, TRUE, sigchld_handler, NULL);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen /* make sure we log if child processes died unexpectedly */