1 /* $OpenBSD: proc.h,v 1.6 2015/12/03 13:08:44 reyk 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_PROC_MAX 38 }; 39 40 /* imsg */ 41 struct imsgev { 42 struct imsgbuf ibuf; 43 void (*handler)(int, short, void *); 44 struct event ev; 45 struct privsep_proc *proc; 46 void *data; 47 short events; 48 }; 49 50 #define IMSG_SIZE_CHECK(imsg, p) do { \ 51 if (IMSG_DATA_SIZE(imsg) < sizeof(*p)) \ 52 fatalx("bad length imsg received"); \ 53 } while (0) 54 #define IMSG_DATA_SIZE(imsg) ((imsg)->hdr.len - IMSG_HEADER_SIZE) 55 56 /* control socket */ 57 struct control_sock { 58 const char *cs_name; 59 struct event cs_ev; 60 struct event cs_evt; 61 int cs_fd; 62 int cs_restricted; 63 void *cs_env; 64 65 TAILQ_ENTRY(control_sock) cs_entry; 66 }; 67 TAILQ_HEAD(control_socks, control_sock); 68 69 struct { 70 struct event ev; 71 int fd; 72 } control_state; 73 74 struct ctl_conn { 75 TAILQ_ENTRY(ctl_conn) entry; 76 uint8_t flags; 77 unsigned int waiting; 78 #define CTL_CONN_NOTIFY 0x01 79 struct imsgev iev; 80 struct sockpeercred peercred; 81 82 }; 83 TAILQ_HEAD(ctl_connlist, ctl_conn); 84 extern struct ctl_connlist ctl_conns; 85 86 /* privsep */ 87 enum privsep_procid { 88 PROC_PARENT = 0, 89 PROC_CONTROL, 90 PROC_VMM, 91 PROC_MAX, 92 } privsep_process; 93 94 #define CONFIG_RELOAD 0x00 95 #define CONFIG_VMS 0x01 96 #define CONFIG_ALL 0xff 97 98 struct privsep_pipes { 99 int *pp_pipes[PROC_MAX]; 100 }; 101 102 struct privsep { 103 struct privsep_pipes *ps_pipes[PROC_MAX]; 104 struct privsep_pipes *ps_pp; 105 106 struct imsgev *ps_ievs[PROC_MAX]; 107 const char *ps_title[PROC_MAX]; 108 pid_t ps_pid[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_ninstances; 119 unsigned int ps_instance; 120 121 /* Event and signal handlers */ 122 struct event ps_evsigint; 123 struct event ps_evsigterm; 124 struct event ps_evsigchld; 125 struct event ps_evsighup; 126 struct event ps_evsigpipe; 127 struct event ps_evsigusr1; 128 129 void *ps_env; 130 }; 131 132 struct privsep_proc { 133 const char *p_title; 134 enum privsep_procid p_id; 135 int (*p_cb)(int, struct privsep_proc *, 136 struct imsg *); 137 pid_t (*p_init)(struct privsep *, 138 struct privsep_proc *); 139 const char *p_chroot; 140 struct privsep *p_ps; 141 void *p_env; 142 void (*p_shutdown)(void); 143 unsigned int p_instance; 144 }; 145 146 /* proc.c */ 147 void proc_init(struct privsep *, struct privsep_proc *, unsigned int); 148 void proc_kill(struct privsep *); 149 void proc_listen(struct privsep *, struct privsep_proc *, size_t); 150 void proc_dispatch(int, short event, void *); 151 pid_t proc_run(struct privsep *, struct privsep_proc *, 152 struct privsep_proc *, unsigned int, 153 void (*)(struct privsep *, struct privsep_proc *, void *), void *); 154 void imsg_event_add(struct imsgev *); 155 int imsg_compose_event(struct imsgev *, uint16_t, uint32_t, 156 pid_t, int, void *, uint16_t); 157 int imsg_composev_event(struct imsgev *, uint16_t, uint32_t, 158 pid_t, int, const struct iovec *, int); 159 int proc_compose_imsg(struct privsep *, enum privsep_procid, int, 160 uint16_t, uint32_t, int, void *, uint16_t); 161 int proc_compose(struct privsep *, enum privsep_procid, 162 uint16_t, void *data, uint16_t); 163 int proc_composev_imsg(struct privsep *, enum privsep_procid, int, 164 uint16_t, uint32_t, int, const struct iovec *, int); 165 int proc_composev(struct privsep *, enum privsep_procid, 166 uint16_t, const struct iovec *, int); 167 int proc_forward_imsg(struct privsep *, struct imsg *, 168 enum privsep_procid, int); 169 struct imsgbuf * 170 proc_ibuf(struct privsep *, enum privsep_procid, int); 171 struct imsgev * 172 proc_iev(struct privsep *, enum privsep_procid, int); 173 174 /* control.c */ 175 pid_t control(struct privsep *, struct privsep_proc *); 176 int control_init(struct privsep *, struct control_sock *); 177 int control_listen(struct control_sock *); 178 void control_cleanup(struct control_sock *); 179 180 /* log.c */ 181 void log_init(int, int); 182 void log_procinit(const char *); 183 void log_verbose(int); 184 void log_warn(const char *, ...) 185 __attribute__((__format__ (printf, 1, 2))); 186 void log_warnx(const char *, ...) 187 __attribute__((__format__ (printf, 1, 2))); 188 void log_info(const char *, ...) 189 __attribute__((__format__ (printf, 1, 2))); 190 void log_debug(const char *, ...) 191 __attribute__((__format__ (printf, 1, 2))); 192 void logit(int, const char *, ...) 193 __attribute__((__format__ (printf, 2, 3))); 194 void vlog(int, const char *, va_list) 195 __attribute__((__format__ (printf, 2, 0))); 196 __dead void fatal(const char *, ...) 197 __attribute__((__format__ (printf, 1, 2))); 198 __dead void fatalx(const char *, ...) 199 __attribute__((__format__ (printf, 1, 2))); 200 201 #endif /* _PROC_H */ 202