| 1 |
/* -*- linux-c -*- |
| 2 |
* |
| 3 |
* staprun.h - include file for staprun and stapio |
| 4 |
* |
| 5 |
* This file is part of systemtap, and is free software. You can |
| 6 |
* redistribute it and/or modify it under the terms of the GNU General |
| 7 |
* Public License (GPL); either version 2, or (at your option) any |
| 8 |
* later version. |
| 9 |
* |
| 10 |
* Copyright (C) 2005-2021 Red Hat Inc. |
| 11 |
*/ |
| 12 |
#define _FILE_OFFSET_BITS 64 |
| 13 |
|
| 14 |
/* kernel-headers and glibc like to stomp on each other. We include this early |
| 15 |
* so we can ensure glibc's own definitions will win. rhbz 837641 & 840902 */ |
| 16 |
#include <linux/posix_types.h> |
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <ctype.h> |
| 21 |
#include <unistd.h> |
| 22 |
#include <dirent.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/stat.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <string.h> |
| 27 |
#include <stdint.h> |
| 28 |
#include <signal.h> |
| 29 |
#include <sys/ioctl.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <linux/fd.h> |
| 32 |
#include <sys/mman.h> |
| 33 |
#include <sys/poll.h> |
| 34 |
#include <pthread.h> |
| 35 |
#include <sys/socket.h> |
| 36 |
#include <linux/limits.h> |
| 37 |
#include <sys/wait.h> |
| 38 |
#include <sys/statfs.h> |
| 39 |
#include <syslog.h> |
| 40 |
#include <sys/sysinfo.h> |
| 41 |
|
| 42 |
/* Include config.h to pick up dependency for --prefix usage. */ |
| 43 |
#include "../config.h" |
| 44 |
#include "../privilege.h" |
| 45 |
|
| 46 |
#if ENABLE_NLS |
| 47 |
#include <libintl.h> |
| 48 |
#include <locale.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
/* define gettext options if NLS is set */ |
| 53 |
#if ENABLE_NLS |
| 54 |
#define _(string) gettext(string) |
| 55 |
#define _N(string, string_plural, count) \ |
| 56 |
ngettext((string), (string_plural), (count)) |
| 57 |
#else |
| 58 |
#define _(string) (string) |
| 59 |
#define _N(string, string_plural, count) \ |
| 60 |
( (count) == 1 ? (string) : (string_plural) ) |
| 61 |
#endif |
| 62 |
/* NB: _F and _NF not available for staprun, since autosprintf() is defined |
| 63 |
in c++ in ../util.cxx, in a memory-leak-free way. */ |
| 64 |
#if 0 |
| 65 |
#define _F(format, ...) autosprintf(_(format), __VA_ARGS__) |
| 66 |
#define _NF(format, format_plural, count, ...) \ |
| 67 |
autosprintf(_N((format), (format_plural), (count)), __VA_ARGS__) |
| 68 |
#endif |
| 69 |
|
| 70 |
/* For probes in staprun.c, staprun_funcs.c, mainloop.c and common.c */ |
| 71 |
#include "stap-probe.h" |
| 72 |
|
| 73 |
extern void eprintf(const char *fmt, ...); |
| 74 |
extern void switch_syslog(const char *name); |
| 75 |
extern char *parse_stap_color(const char *type); |
| 76 |
|
| 77 |
extern char *__name__; |
| 78 |
|
| 79 |
/* print to stderr */ |
| 80 |
#define COLOR_FMT "\033[%sm\033[K" |
| 81 |
#define COLOR_RESET "\033[m\033[K" |
| 82 |
#define print_stderr(color, tag, fmt, ...) \ |
| 83 |
do { \ |
| 84 |
char *f, *seq; \ |
| 85 |
int num; \ |
| 86 |
\ |
| 87 |
/* Build the final format string so there's only one eprintf() call */ \ |
| 88 |
seq = color && color_errors ? parse_stap_color(color) : NULL; \ |
| 89 |
if (seq) \ |
| 90 |
num = snprintf(NULL, 0, COLOR_FMT "%s" COLOR_RESET " %s", seq, \ |
| 91 |
tag, fmt); \ |
| 92 |
else \ |
| 93 |
num = snprintf(NULL, 0, "%s %s", tag, fmt); \ |
| 94 |
\ |
| 95 |
f = malloc(num + 1); \ |
| 96 |
if (!f) { \ |
| 97 |
fprintf(stderr, "Memory allocation failed.\n"); \ |
| 98 |
exit(-2); \ |
| 99 |
} \ |
| 100 |
\ |
| 101 |
if (seq) { \ |
| 102 |
sprintf(f, COLOR_FMT "%s" COLOR_RESET " %s", seq, tag, fmt); \ |
| 103 |
free(seq); \ |
| 104 |
} else { \ |
| 105 |
sprintf(f, "%s %s", tag, fmt); \ |
| 106 |
} \ |
| 107 |
\ |
| 108 |
eprintf(f, ##__VA_ARGS__); \ |
| 109 |
free(f); \ |
| 110 |
} while (0) |
| 111 |
|
| 112 |
#define err(fmt, ...) \ |
| 113 |
print_stderr("error", _("ERROR:"), fmt, ##__VA_ARGS__) |
| 114 |
|
| 115 |
#define warn(fmt, ...) \ |
| 116 |
print_stderr("warning", _("WARNING:"), fmt, ##__VA_ARGS__) |
| 117 |
|
| 118 |
#define dbug(level, fmt, ...) \ |
| 119 |
do { \ |
| 120 |
if (verbose >= level) \ |
| 121 |
print_stderr(NULL, "%s:%s:%d", fmt, __name__, \ |
| 122 |
__FUNCTION__, __LINE__, ##__VA_ARGS__); \ |
| 123 |
} while (0) |
| 124 |
|
| 125 |
/* better perror() */ |
| 126 |
#define perr(args...) do { \ |
| 127 |
int _errno = errno; \ |
| 128 |
err(args); \ |
| 129 |
eprintf(": %s\n", strerror(_errno)); \ |
| 130 |
} while (0) |
| 131 |
|
| 132 |
/* Error messages. Use these for serious errors, not informational messages to stderr. */ |
| 133 |
#define _err(fmt, ...) \ |
| 134 |
print_stderr(NULL, "%s:%s:%d: ERROR:", fmt, __name__, \ |
| 135 |
__FUNCTION__, __LINE__, ##__VA_ARGS__) |
| 136 |
#define _perr(args...) do { \ |
| 137 |
int _errno = errno; \ |
| 138 |
_err(args); \ |
| 139 |
eprintf(": %s\n", strerror(_errno)); \ |
| 140 |
} while (0) |
| 141 |
#define overflow_error() _err("Internal buffer overflow. Please file a bug report.\n") |
| 142 |
|
| 143 |
/* Error checking version of sprintf() - returns 1 if overflow error */ |
| 144 |
#define sprintf_chk(str, args...) ({ \ |
| 145 |
int _rc; \ |
| 146 |
_rc = snprintf(str, sizeof(str), args); \ |
| 147 |
if (_rc >= (int)sizeof(str)) { \ |
| 148 |
overflow_error(); \ |
| 149 |
_rc = 1; \ |
| 150 |
} \ |
| 151 |
else \ |
| 152 |
_rc = 0; \ |
| 153 |
_rc; \ |
| 154 |
}) |
| 155 |
|
| 156 |
/* Error checking version of snprintf() - returns 1 if overflow error */ |
| 157 |
#define snprintf_chk(str, size, args...) ({ \ |
| 158 |
int _rc; \ |
| 159 |
_rc = snprintf(str, size, args); \ |
| 160 |
if (_rc >= (int)size) { \ |
| 161 |
overflow_error(); \ |
| 162 |
_rc = 1; \ |
| 163 |
} \ |
| 164 |
else \ |
| 165 |
_rc = 0; \ |
| 166 |
_rc; \ |
| 167 |
}) |
| 168 |
|
| 169 |
/* Grabbed from linux/module.h kernel include. */ |
| 170 |
#define MODULE_NAME_LEN (64 - sizeof(unsigned long)) |
| 171 |
|
| 172 |
#include "../runtime/transport/transport_msgs.h" |
| 173 |
|
| 174 |
#define RELAYFS_MAGIC 0xF0B4A981 |
| 175 |
#define DEBUGFS_MAGIC 0x64626720 |
| 176 |
#define DEBUGFSDIR "/sys/kernel/debug" |
| 177 |
#define RELAYFSDIR "/mnt/relay" |
| 178 |
|
| 179 |
/* |
| 180 |
* function prototypes |
| 181 |
*/ |
| 182 |
int init_staprun(void); |
| 183 |
int init_stapio(void); |
| 184 |
int stp_main_loop(void); |
| 185 |
int send_request(int type, void *data, int len); |
| 186 |
void cleanup_and_exit (int, int); |
| 187 |
int init_ctl_channel(const char *name, int verb); |
| 188 |
void close_ctl_channel(void); |
| 189 |
int init_relayfs(void); |
| 190 |
void close_relayfs(void); |
| 191 |
void kill_relayfs(void); |
| 192 |
int init_oldrelayfs(void); |
| 193 |
void close_oldrelayfs(int); |
| 194 |
int write_realtime_data(void *data, ssize_t nb); |
| 195 |
void setup_signals(void); |
| 196 |
int make_outfile_name(char *buf, int max, int fnum, int cpu, |
| 197 |
time_t t, int bulk); |
| 198 |
int init_backlog(int cpu); |
| 199 |
void write_backlog(int cpu, int fnum, time_t t); |
| 200 |
time_t read_backlog(int cpu, int fnum); |
| 201 |
void read_stdin_setup(void); |
| 202 |
void read_stdin_cleanup(void); |
| 203 |
/* staprun_funcs.c */ |
| 204 |
void setup_staprun_signals(void); |
| 205 |
const char *moderror(int err); |
| 206 |
|
| 207 |
/* insert_module helper functions. */ |
| 208 |
typedef void (*assert_permissions_func) ( |
| 209 |
const char *module_path __attribute__ ((unused)), |
| 210 |
int module_fd __attribute__ ((unused)), |
| 211 |
const void *module_data __attribute__ ((unused)), |
| 212 |
off_t module_size __attribute__ ((unused)), |
| 213 |
privilege_t *user_credentials __attribute__ ((unused)) |
| 214 |
); |
| 215 |
|
| 216 |
void assert_stap_module_permissions ( |
| 217 |
const char *module_path __attribute__ ((unused)), |
| 218 |
int module_fd __attribute__ ((unused)), |
| 219 |
const void *module_data __attribute__ ((unused)), |
| 220 |
off_t module_size __attribute__ ((unused)), |
| 221 |
privilege_t *user_credentials __attribute__ ((unused)) |
| 222 |
); |
| 223 |
|
| 224 |
void assert_uprobes_module_permissions ( |
| 225 |
const char *module_path __attribute__ ((unused)), |
| 226 |
int module_fd __attribute__ ((unused)), |
| 227 |
const void *module_data __attribute__ ((unused)), |
| 228 |
off_t module_size __attribute__ ((unused)), |
| 229 |
privilege_t *user_credentials __attribute__ ((unused)) |
| 230 |
); |
| 231 |
|
| 232 |
int insert_module(const char *path, const char *special_options, |
| 233 |
char **options, assert_permissions_func apf, |
| 234 |
privilege_t *user_credentials |
| 235 |
); |
| 236 |
|
| 237 |
int rename_module(void* module_file, const __off_t st_size); |
| 238 |
|
| 239 |
int mountfs(void); |
| 240 |
void start_symbol_thread(void); |
| 241 |
void stop_symbol_thread(void); |
| 242 |
|
| 243 |
/* common.c functions */ |
| 244 |
int stap_strfloctime(char *buf, size_t max, const char *fmt, time_t t); |
| 245 |
void parse_args(int argc, char **argv); |
| 246 |
void usage(char *prog, int rc); |
| 247 |
void parse_modpath(const char *); |
| 248 |
void setup_signals(void); |
| 249 |
int set_clexec(int fd); |
| 250 |
int open_cloexec(const char *pathname, int flags, mode_t mode); |
| 251 |
#ifdef HAVE_OPENAT |
| 252 |
int openat_cloexec(int dirfd, const char *pathname, int flags, mode_t mode); |
| 253 |
#endif |
| 254 |
int pipe_cloexec(int pipefd[2]); |
| 255 |
|
| 256 |
/* start_cmd.c functions; XXX shared with ../stapbpf */ |
| 257 |
void closefrom(int lowfd); |
| 258 |
void start_cmd(void); |
| 259 |
int resume_cmd(void); |
| 260 |
|
| 261 |
/* monitor.c function */ |
| 262 |
void monitor_winch(int signum); |
| 263 |
void monitor_setup(void); |
| 264 |
void monitor_cleanup(void); |
| 265 |
void monitor_render(void); |
| 266 |
void monitor_input(void); |
| 267 |
void monitor_exited(void); |
| 268 |
void monitor_remember_output_line(const char* buf, const size_t bytes); |
| 269 |
|
| 270 |
/* |
| 271 |
* variables |
| 272 |
*/ |
| 273 |
extern int control_channel; |
| 274 |
extern int ncpus; |
| 275 |
extern int initialized; |
| 276 |
extern int kernel_ptr_size; |
| 277 |
extern int monitor_pfd[2]; |
| 278 |
extern int monitor_end; |
| 279 |
|
| 280 |
/* flags */ |
| 281 |
extern int verbose; |
| 282 |
extern int suppress_warnings; |
| 283 |
extern unsigned int buffer_size; |
| 284 |
extern unsigned int reader_timeout_ms; |
| 285 |
extern char *modname; |
| 286 |
extern char *modpath; |
| 287 |
#define MAXMODOPTIONS 64 |
| 288 |
extern char *modoptions[MAXMODOPTIONS]; |
| 289 |
extern int target_pid; |
| 290 |
extern char *target_cmd; |
| 291 |
extern int target_namespaces_pid; |
| 292 |
extern int target_mnt_ns_fd; |
| 293 |
extern int orig_mnt_ns_fd; |
| 294 |
extern char *outfile_name; |
| 295 |
extern int read_stdin; |
| 296 |
extern int rename_mod; |
| 297 |
extern int attach_mod; |
| 298 |
extern int delete_mod; |
| 299 |
extern int load_only; |
| 300 |
extern int need_uprobes; |
| 301 |
extern const char *uprobes_path; |
| 302 |
extern int daemon_mode; |
| 303 |
extern off_t fsize_max; |
| 304 |
extern int fnum_max; |
| 305 |
extern int remote_id; |
| 306 |
extern const char *remote_uri; |
| 307 |
extern int relay_basedir_fd; |
| 308 |
extern int color_errors; |
| 309 |
extern int monitor; |
| 310 |
extern int monitor_interval; |
| 311 |
|
| 312 |
typedef enum {color_never, color_auto, color_always} color_modes; |
| 313 |
extern color_modes color_mode; |
| 314 |
|
| 315 |
/* getopt variables */ |
| 316 |
extern char *optarg; |
| 317 |
extern int optopt; |
| 318 |
extern int optind; |
| 319 |
|
| 320 |
/* maximum number of CPUs we can handle */ |
| 321 |
#define MAX_NR_CPUS 1024 |
| 322 |
|
| 323 |
/* relay*.c uses these */ |
| 324 |
extern int out_fd[MAX_NR_CPUS]; |
| 325 |
|
| 326 |
/* |
| 327 |
* ppoll exists in glibc >= 2.4 |
| 328 |
*/ |
| 329 |
#if (__GLIBC__ < 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ < 4)) |
| 330 |
#define NEED_PPOLL |
| 331 |
|
| 332 |
extern int ppoll(struct pollfd *fds, nfds_t nfds, |
| 333 |
const struct timespec *timeout, const sigset_t *sigmask); |
| 334 |
#endif |