1 /*
2  * Copyright (c) 2014 DeNA Co., Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 #ifndef h2o__server_starter_h
23 #define h2o__server_starter_h
24 
25 #include <stddef.h>
26 
27 /* taken from sysexits.h */
28 #ifndef EX_SOFTWARE
29 #define EX_SOFTWARE 70
30 #endif
31 #ifndef EX_OSERR
32 #define EX_OSERR 71
33 #endif
34 #ifndef EX_TEMPFAIL
35 #define EX_TEMPFAIL 75
36 #endif
37 #ifndef EX_CONFIG
38 #define EX_CONFIG 78
39 #endif
40 
41 /**
42  * environment variable name for listening socket's addr and fd
43  * example: 127.0.0.1:80=3;/tmp/sock=4
44  */
45 #define SERVER_STARTER_PORT "SERVER_STARTER_PORT"
46 
47 /**
48  * equivalent of signal(3)
49  */
50 void h2o_set_signal_handler(int signo, void (*cb)(int signo));
51 
52 /**
53  * equiv. to setuidgid of djb
54  */
55 int h2o_setuidgid(const char *user);
56 
57 /**
58  * return a list of fds passed in from Server::Starter, or 0 if Server::Starter was not used.  -1 on error
59  */
60 size_t h2o_server_starter_get_fds(int **_fds);
61 
62 /**
63  * spawns a command with given arguments, while mapping the designated file descriptors.
64  * @param cmd file being executed
65  * @param argv argv passed to the executable
66  * @param mapped_fds if non-NULL, must point to an array contain containing a list of pair of file descriptors, terminated with -1.
67  *        Every pair of the mapping will be duplicated by calling `dup2` before execvp is being called if the second value of the
68  *        pair is not -1.  If the second value is -1, then `close` is called with the first value as the argument.
69  * @return pid of the process being spawned if successful, or -1 if otherwise
70  */
71 pid_t h2o_spawnp(const char *cmd, char *const *argv, const int *mapped_fds, int clocexec_mutex_is_locked);
72 
73 /**
74  * executes a command and returns its output
75  * @param cmd
76  * @param argv
77  * @param std_in data to be fed into the standard input of the command
78  * @param resp the output, only available if the function returns zero
79  * @param child_status result of waitpid(child_pid), only available if the function returns zero
80  */
81 int h2o_read_command(const char *cmd, char **argv, h2o_iovec_t std_in, h2o_buffer_t **resp, int *child_status);
82 
83 /**
84  * Gets the number of processor cores
85  */
86 size_t h2o_numproc(void);
87 
88 #endif
89