1 /* $OpenBSD: proc.h,v 1.16 2018/09/10 10:36:01 bluhm Exp $ */ 2 3 /* 4 * Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/socket.h> 20 #include <sys/queue.h> 21 #include <sys/uio.h> 22 23 #include <imsg.h> 24 #include <event.h> 25 26 #ifndef _PROC_H 27 #define _PROC_H 28 29 enum { 30 IMSG_NONE, 31 IMSG_CTL_OK, 32 IMSG_CTL_FAIL, 33 IMSG_CTL_VERBOSE, 34 IMSG_CTL_END, 35 IMSG_CTL_NOTIFY, 36 IMSG_CTL_RESET, 37 IMSG_CTL_PROCFD, 38 IMSG_PROC_MAX 39 }; 40 41 /* imsg */ 42 struct imsgev { 43 struct imsgbuf ibuf; 44 void (*handler)(int, short, void *); 45 struct event ev; 46 struct privsep_proc *proc; 47 void *data; 48 short events; 49 }; 50 51 #define IMSG_SIZE_CHECK(imsg, p) do { \ 52 if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \ 53 fatalx("bad length imsg received (%s)", #p); \ 54 } while (0) 55 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE) 56 57 /* control socket */ 58 struct control_sock { 59 const char *cs_name; 60 struct event cs_ev; 61 struct event cs_evt; 62 int cs_fd; 63 int cs_restricted; 64 void *cs_env; 65 uid_t cs_uid; 66 gid_t cs_gid; 67 68 TAILQ_ENTRY(control_sock) cs_entry; 69 }; 70 TAILQ_HEAD(control_socks, control_sock); 71 72 struct ctl_conn { 73 TAILQ_ENTRY(ctl_conn) entry; 74 uint8_t flags; 75 unsigned int waiting; 76 #define CTL_CONN_NOTIFY 0x01 77 struct imsgev iev; 78 struct sockpeercred peercred; 79 80 }; 81 TAILQ_HEAD(ctl_connlist, ctl_conn); 82 extern struct ctl_connlist ctl_conns; 83 84 /* privsep */ 85 enum privsep_procid { 86 PROC_PARENT = 0, 87 PROC_CONTROL, 88 PROC_VMM, 89 PROC_PRIV, 90 PROC_MAX, 91 } privsep_process; 92 93 #define CONFIG_RELOAD 0x00 94 #define CONFIG_VMS 0x01 95 #define CONFIG_SWITCHES 0x02 96 #define CONFIG_USERS 0x04 97 #define CONFIG_ALL 0xff 98 99 struct privsep_pipes { 100 int *pp_pipes[PROC_MAX]; 101 }; 102 103 struct privsep { 104 struct privsep_pipes *ps_pipes[PROC_MAX]; 105 struct privsep_pipes *ps_pp; 106 107 struct imsgev *ps_ievs[PROC_MAX]; 108 const char *ps_title[PROC_MAX]; 109 uint8_t ps_what[PROC_MAX]; 110 111 struct passwd *ps_pw; 112 int ps_noaction; 113 114 struct control_sock ps_csock; 115 struct control_socks ps_rcsocks; 116 117 unsigned int ps_instances[PROC_MAX]; 118 unsigned int ps_instance; 119 120 /* Event and signal handlers */ 121 struct event ps_evsigint; 122 struct event ps_evsigterm; 123 struct event ps_evsigchld; 124 struct event ps_evsighup; 125 struct event ps_evsigpipe; 126 struct event ps_evsigusr1; 127 128 void *ps_env; 129 }; 130 131 struct privsep_proc { 132 const char *p_title; 133 enum privsep_procid p_id; 134 int (*p_cb)(int, struct privsep_proc *, 135 struct imsg *); 136 void (*p_init)(struct privsep *, 137 struct privsep_proc *); 138 void (*p_shutdown)(void); 139 const char *p_chroot; 140 struct passwd *p_pw; 141 struct privsep *p_ps; 142 }; 143 144 struct privsep_fd { 145 enum privsep_procid pf_procid; 146 unsigned int pf_instance; 147 }; 148 149 #if DEBUG 150 #define DPRINTF log_debug 151 #else 152 #define DPRINTF(x...) do {} while(0) 153 #endif 154 155 #define PROC_PARENT_SOCK_FILENO 3 156 #define PROC_MAX_INSTANCES 32 157 158 /* proc.c */ 159 void proc_init(struct privsep *, struct privsep_proc *, unsigned int, int, 160 int, char **, enum privsep_procid); 161 void proc_kill(struct privsep *); 162 void proc_connect(struct privsep *ps); 163 void proc_dispatch(int, short event, void *); 164 void proc_run(struct privsep *, struct privsep_proc *, 165 struct privsep_proc *, unsigned int, 166 void (*)(struct privsep *, struct privsep_proc *, void *), void *); 167 void imsg_event_add(struct imsgev *); 168 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, 169 pid_t, int, void *, uint16_t); 170 int imsg_composev_event(struct imsgev *, uint16_t, uint32_t, 171 pid_t, int, const struct iovec *, int); 172 int proc_compose_imsg(struct privsep *, enum privsep_procid, int, 173 uint16_t, uint32_t, int, void *, uint16_t); 174 int proc_compose(struct privsep *, enum privsep_procid, 175 uint16_t, void *data, uint16_t); 176 int proc_composev_imsg(struct privsep *, enum privsep_procid, int, 177 uint16_t, uint32_t, int, const struct iovec *, int); 178 int proc_composev(struct privsep *, enum privsep_procid, 179 uint16_t, const struct iovec *, int); 180 int proc_forward_imsg(struct privsep *, struct imsg *, 181 enum privsep_procid, int); 182 struct imsgbuf * 183 proc_ibuf(struct privsep *, enum privsep_procid, int); 184 struct imsgev * 185 proc_iev(struct privsep *, enum privsep_procid, int); 186 enum privsep_procid 187 proc_getid(struct privsep_proc *, unsigned int, const char *); 188 int proc_flush_imsg(struct privsep *, enum privsep_procid, int); 189 190 /* control.c */ 191 void control(struct privsep *, struct privsep_proc *); 192 int control_init(struct privsep *, struct control_sock *); 193 int control_reset(struct control_sock *); 194 int control_listen(struct control_sock *); 195 196 /* log.c */ 197 void log_init(int, int); 198 void log_procinit(const char *); 199 void log_setverbose(int); 200 int log_getverbose(void); 201 void log_warn(const char *, ...) 202 __attribute__((__format__ (printf, 1, 2))); 203 void log_warnx(const char *, ...) 204 __attribute__((__format__ (printf, 1, 2))); 205 void log_info(const char *, ...) 206 __attribute__((__format__ (printf, 1, 2))); 207 void log_debug(const char *, ...) 208 __attribute__((__format__ (printf, 1, 2))); 209 void logit(int, const char *, ...) 210 __attribute__((__format__ (printf, 2, 3))); 211 void vlog(int, const char *, va_list) 212 __attribute__((__format__ (printf, 2, 0))); 213 __dead void fatal(const char *, ...) 214 __attribute__((__format__ (printf, 1, 2))); 215 __dead void fatalx(const char *, ...) 216 __attribute__((__format__ (printf, 1, 2))); 217 218 #endif /* _PROC_H */ 219