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 | |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <common/mini-clist.h> |
| 17 | |
| 18 | #include <proto/mworker.h> |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame^] | 19 | #include <proto/signal.h> |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 20 | |
| 21 | #include <types/global.h> |
| 22 | |
| 23 | |
William Lallemand | 48dfbbd | 2019-04-01 11:29:53 +0200 | [diff] [blame] | 24 | /* |
| 25 | * serialize the proc list and put it in the environment |
| 26 | */ |
| 27 | void mworker_proc_list_to_env() |
| 28 | { |
| 29 | char *msg = NULL; |
| 30 | struct mworker_proc *child; |
| 31 | |
| 32 | list_for_each_entry(child, &proc_list, list) { |
| 33 | if (child->pid > -1) |
| 34 | 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); |
| 35 | } |
| 36 | if (msg) |
| 37 | setenv("HAPROXY_PROCESSES", msg, 1); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * unserialize the proc list from the environment |
| 42 | */ |
| 43 | void mworker_env_to_proc_list() |
| 44 | { |
| 45 | char *msg, *token = NULL, *s1; |
| 46 | |
| 47 | msg = getenv("HAPROXY_PROCESSES"); |
| 48 | if (!msg) |
| 49 | return; |
| 50 | |
| 51 | while ((token = strtok_r(msg, "|", &s1))) { |
| 52 | struct mworker_proc *child; |
| 53 | char *subtoken = NULL; |
| 54 | char *s2; |
| 55 | |
| 56 | msg = NULL; |
| 57 | |
| 58 | child = calloc(1, sizeof(*child)); |
| 59 | |
| 60 | while ((subtoken = strtok_r(token, ";", &s2))) { |
| 61 | |
| 62 | token = NULL; |
| 63 | |
| 64 | if (strncmp(subtoken, "type=", 5) == 0) { |
| 65 | child->type = *(subtoken+5); |
| 66 | if (child->type == 'm') /* we are in the master, assign it */ |
| 67 | proc_self = child; |
| 68 | } else if (strncmp(subtoken, "fd=", 3) == 0) { |
| 69 | child->ipc_fd[0] = atoi(subtoken+3); |
| 70 | } else if (strncmp(subtoken, "pid=", 4) == 0) { |
| 71 | child->pid = atoi(subtoken+4); |
| 72 | } else if (strncmp(subtoken, "rpid=", 5) == 0) { |
| 73 | child->relative_pid = atoi(subtoken+5); |
| 74 | } else if (strncmp(subtoken, "reloads=", 8) == 0) { |
| 75 | /* we reloaded this process once more */ |
| 76 | child->reloads = atoi(subtoken+8) + 1; |
| 77 | } else if (strncmp(subtoken, "timestamp=", 10) == 0) { |
| 78 | child->timestamp = atoi(subtoken+10); |
| 79 | } |
| 80 | } |
| 81 | if (child->pid) |
| 82 | LIST_ADDQ(&proc_list, &child->list); |
| 83 | else |
| 84 | free(child); |
| 85 | } |
| 86 | |
| 87 | unsetenv("HAPROXY_PROCESSES"); |
| 88 | } |
William Lallemand | 3cd95d2 | 2019-04-01 11:29:54 +0200 | [diff] [blame^] | 89 | |
| 90 | /* Signal blocking and unblocking */ |
| 91 | |
| 92 | void mworker_block_signals() |
| 93 | { |
| 94 | sigset_t set; |
| 95 | |
| 96 | sigemptyset(&set); |
| 97 | sigaddset(&set, SIGUSR1); |
| 98 | sigaddset(&set, SIGUSR2); |
| 99 | sigaddset(&set, SIGHUP); |
| 100 | sigaddset(&set, SIGCHLD); |
| 101 | ha_sigmask(SIG_SETMASK, &set, NULL); |
| 102 | } |
| 103 | |
| 104 | void mworker_unblock_signals() |
| 105 | { |
| 106 | haproxy_unblock_signals(); |
| 107 | } |