William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Master Worker |
| 3 | * |
| 4 | * Copyright HAProxy Technologies 2019 - William Lallemand <wlallemand@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame^] | 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <signal.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include <common/mini-clist.h> |
| 20 | |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame^] | 21 | #include <proto/fd.h> |
| 22 | #include <proto/listener.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 23 | #include <proto/mworker.h> |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame] | 24 | #include <proto/signal.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 25 | |
| 26 | #include <types/global.h> |
| 27 | |
| 28 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 29 | /* |
| 30 | * serialize the proc list and put it in the environment |
| 31 | */ |
| 32 | void mworker_proc_list_to_env() |
| 33 | { |
| 34 | char *msg = NULL; |
| 35 | struct mworker_proc *child; |
| 36 | |
| 37 | list_for_each_entry(child, &proc_list, list) { |
| 38 | if (child->pid > -1) |
| 39 | memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", msg ? msg : "", child->type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp); |
| 40 | } |
| 41 | if (msg) |
| 42 | setenv("HAPROXY_PROCESSES", msg, 1); |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | * unserialize the proc list from the environment |
| 47 | */ |
| 48 | void mworker_env_to_proc_list() |
| 49 | { |
| 50 | char *msg, *token = NULL, *s1; |
| 51 | |
| 52 | msg = getenv("HAPROXY_PROCESSES"); |
| 53 | if (!msg) |
| 54 | return; |
| 55 | |
| 56 | while ((token = strtok_r(msg, "|", &s1))) { |
| 57 | struct mworker_proc *child; |
| 58 | char *subtoken = NULL; |
| 59 | char *s2; |
| 60 | |
| 61 | msg = NULL; |
| 62 | |
| 63 | child = calloc(1, sizeof(*child)); |
| 64 | |
| 65 | while ((subtoken = strtok_r(token, ";", &s2))) { |
| 66 | |
| 67 | token = NULL; |
| 68 | |
| 69 | if (strncmp(subtoken, "type=", 5) == 0) { |
| 70 | child->type = *(subtoken+5); |
| 71 | if (child->type == 'm') /* we are in the master, assign it */ |
| 72 | proc_self = child; |
| 73 | } else if (strncmp(subtoken, "fd=", 3) == 0) { |
| 74 | child->ipc_fd[0] = atoi(subtoken+3); |
| 75 | } else if (strncmp(subtoken, "pid=", 4) == 0) { |
| 76 | child->pid = atoi(subtoken+4); |
| 77 | } else if (strncmp(subtoken, "rpid=", 5) == 0) { |
| 78 | child->relative_pid = atoi(subtoken+5); |
| 79 | } else if (strncmp(subtoken, "reloads=", 8) == 0) { |
| 80 | /* we reloaded this process once more */ |
| 81 | child->reloads = atoi(subtoken+8) + 1; |
| 82 | } else if (strncmp(subtoken, "timestamp=", 10) == 0) { |
| 83 | child->timestamp = atoi(subtoken+10); |
| 84 | } |
| 85 | } |
| 86 | if (child->pid) |
| 87 | LIST_ADDQ(&proc_list, &child->list); |
| 88 | else |
| 89 | free(child); |
| 90 | } |
| 91 | |
| 92 | unsetenv("HAPROXY_PROCESSES"); |
| 93 | } |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame] | 94 | |
| 95 | /* Signal blocking and unblocking */ |
| 96 | |
| 97 | void mworker_block_signals() |
| 98 | { |
| 99 | sigset_t set; |
| 100 | |
| 101 | sigemptyset(&set); |
| 102 | sigaddset(&set, SIGUSR1); |
| 103 | sigaddset(&set, SIGUSR2); |
| 104 | sigaddset(&set, SIGHUP); |
| 105 | sigaddset(&set, SIGCHLD); |
| 106 | ha_sigmask(SIG_SETMASK, &set, NULL); |
| 107 | } |
| 108 | |
| 109 | void mworker_unblock_signals() |
| 110 | { |
| 111 | haproxy_unblock_signals(); |
| 112 | } |
William Lallemand | 3fa724d | 2019-04-01 11:29:55 +0200 | [diff] [blame^] | 113 | |
| 114 | /* ----- IPC FD (sockpair) related ----- */ |
| 115 | |
| 116 | /* This wrapper is called from the workers. It is registered instead of the |
| 117 | * normal listener_accept() so the worker can exit() when it detects that the |
| 118 | * master closed the IPC FD. If it's not a close, we just call the regular |
| 119 | * listener_accept() function */ |
| 120 | void mworker_accept_wrapper(int fd) |
| 121 | { |
| 122 | char c; |
| 123 | int ret; |
| 124 | |
| 125 | while (1) { |
| 126 | ret = recv(fd, &c, 1, MSG_PEEK); |
| 127 | if (ret == -1) { |
| 128 | if (errno == EINTR) |
| 129 | continue; |
| 130 | if (errno == EAGAIN) { |
| 131 | fd_cant_recv(fd); |
| 132 | return; |
| 133 | } |
| 134 | break; |
| 135 | } else if (ret > 0) { |
| 136 | listener_accept(fd); |
| 137 | return; |
| 138 | } else if (ret == 0) { |
| 139 | /* At this step the master is down before |
| 140 | * this worker perform a 'normal' exit. |
| 141 | * So we want to exit with an error but |
| 142 | * other threads could currently process |
| 143 | * some stuff so we can't perform a clean |
| 144 | * deinit(). |
| 145 | */ |
| 146 | exit(EXIT_FAILURE); |
| 147 | } |
| 148 | } |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * This function register the accept wrapper for the sockpair of the master worker |
| 154 | */ |
| 155 | void mworker_pipe_register() |
| 156 | { |
| 157 | /* The iocb should be already initialized with listener_accept */ |
| 158 | if (fdtab[proc_self->ipc_fd[1]].iocb == mworker_accept_wrapper) |
| 159 | return; |
| 160 | |
| 161 | fcntl(proc_self->ipc_fd[1], F_SETFL, O_NONBLOCK); |
| 162 | /* In multi-tread, we need only one thread to process |
| 163 | * events on the pipe with master |
| 164 | */ |
| 165 | fd_insert(proc_self->ipc_fd[1], fdtab[proc_self->ipc_fd[1]].owner, mworker_accept_wrapper, 1); |
| 166 | fd_want_recv(proc_self->ipc_fd[1]); |
| 167 | } |