libreport  2.13.1
A tool to inform users about various problems on the running system
internal_libreport.h
1 /*
2  Copyright (C) 2010 ABRT team
3  Copyright (C) 2010 RedHat Inc
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef LIBREPORT_INTERNAL_H_
21 #define LIBREPORT_INTERNAL_H_
22 
23 #include <assert.h>
24 #include <ctype.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include <setjmp.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stddef.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <sys/poll.h>
38 #include <sys/mman.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h> /* sockaddr_in, sockaddr_in6 etc */
45 #include <termios.h>
46 #include <time.h>
47 #include <unistd.h>
48 /* Try to pull in PATH_MAX */
49 #include <limits.h>
50 #include <sys/param.h>
51 #ifndef PATH_MAX
52 # define PATH_MAX 256
53 #endif
54 #include <pwd.h>
55 #include <grp.h>
56 
57 #ifdef HAVE_CONFIG_H
58 # include "config.h"
59 #endif
60 
61 /* Must be after #include "config.h" */
62 #ifdef ENABLE_NLS
63 # include <libintl.h>
64 # define _(S) dgettext(PACKAGE, S)
65 #else
66 # define _(S) (S)
67 #endif
68 
69 #ifdef HAVE_LOCALE_H
70 # include <locale.h>
71 #endif /* HAVE_LOCALE_H */
72 
73 /* Some libc's forget to declare these, do it ourself */
74 extern char **environ;
75 #if defined(__GLIBC__) && __GLIBC__ < 2
76 int vdprintf(int d, const char *format, va_list ap);
77 #endif
78 
79 #undef NORETURN
80 #define NORETURN __attribute__ ((noreturn))
81 
82 #undef ERR_PTR
83 #define ERR_PTR ((void*)(uintptr_t)1)
84 
85 #undef ARRAY_SIZE
86 #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
87 
88 /* consts used across whole libreport */
89 #define CREATE_PRIVATE_TICKET "ABRT_CREATE_PRIVATE_TICKET"
90 #define STOP_ON_NOT_REPORTABLE "ABRT_STOP_ON_NOT_REPORTABLE"
91 
92 /* path of user's local config, path is relative to user's home */
93 #define USER_HOME_CONFIG_PATH "/.config/libreport"
94 
95 /* Pull in entire public libreport API */
96 #include "global_configuration.h"
97 #include "dump_dir.h"
98 #include "event_config.h"
99 #include "problem_data.h"
100 #include "report.h"
101 #include "run_event.h"
102 #include "workflow.h"
103 #include "file_obj.h"
104 #include "libreport_types.h"
105 #include "reporters.h"
106 
107 #ifdef __cplusplus
108 extern "C" {
109 #endif
110 
111 int libreport_prefixcmp(const char *str, const char *prefix);
112 int libreport_suffixcmp(const char *str, const char *suffix);
113 char *libreport_trim_all_whitespace(const char *str);
114 char *libreport_shorten_string_to_length(const char *str, unsigned length);
115 char *libreport_strtrim(char *str);
116 char *libreport_strtrimch(char *str, int ch);
117 char *libreport_strremovech(char *str, int ch);
118 char *libreport_append_to_malloced_string(char *mstr, const char *append);
119 char *libreport_skip_blank(const char *s);
120 char *libreport_skip_whitespace(const char *s);
121 char *libreport_skip_non_whitespace(const char *s);
122 /* Like strcpy but can copy overlapping strings. */
123 void libreport_overlapping_strcpy(char *dst, const char *src);
124 
125 char *libreport_concat_path_file(const char *path, const char *filename);
126 /*
127  * Used to construct a name in a different directory with the basename
128  * similar to the old name, if possible.
129  */
130 char *libreport_concat_path_basename(const char *path, const char *filename);
131 
132 /* Allows all printable characters except '/',
133  * the string must not exceed 64 characters of length
134  * and must not equal neither "." nor ".." (these strings may appear in the string) */
135 bool libreport_str_is_correct_filename(const char *str);
136 
137 /* A-la fgets, but malloced and of unlimited size */
138 char *libreport_xmalloc_fgets(FILE *file);
139 /* Similar, but removes trailing \n */
140 char *libreport_xmalloc_fgetline(FILE *file);
141 /* Useful for easy reading of various /proc files */
142 char *libreport_xmalloc_fopen_fgetline_fclose(const char *filename);
143 
144 
145 typedef enum {
146  COPYFD_SPARSE = 1 << 0,
147 } libreport_copyfd_flags;
148 
149 /* Writes up to 'size' Bytes from a file descriptor to a file in a directory
150  *
151  * If you need to write all Bytes of the file descriptor, pass 0 as the size.
152  *
153  * @param src The source file descriptor
154  * @param dir_fd A file descriptor for the parent directory of the destination file
155  * @param name The destination file name
156  * @param mode The destination file open mode
157  * @param uid The destination file's uid
158  * @param gid The destination file's gid
159  * @param open_flags The destination file open flags
160  * @param copy_flags libreport_copyfd_flags
161  * @param size The upper limit for written bytes (0 for no limit).
162  * @return Number of read Bytes on success. On errors, return -1 and prints out
163  * reasonable good error messages.
164  */
165 off_t libreport_copyfd_ext_at(int src, int dir_fd, const char *name, int mode,
166  uid_t uid, gid_t gid, int open_flags, int copy_flags, off_t size);
167 
168 /* On error, copyfd_XX prints error messages and returns -1 */
169 off_t libreport_copyfd_eof(int src_fd, int dst_fd, int flags);
170 off_t libreport_copyfd_size(int src_fd, int dst_fd, off_t size, int flags);
171 void libreport_copyfd_exact_size(int src_fd, int dst_fd, off_t size);
172 off_t libreport_copy_file_ext_2at(int src_dir_fd, const char *src_name, int dir_fd, const char *name, int mode, uid_t uid, gid_t gid, int src_flags, int dst_flags);
173 off_t libreport_copy_file_ext_at(const char *src_name, int dir_fd, const char *name, int mode, uid_t uid, gid_t gid, int src_flags, int dst_flags);
174 #define libreport_copy_file_ext(src_name, dst_name, mode, uid, gid, src_flags, dst_flags) \
175  libreport_copy_file_ext_at(src_name, AT_FDCWD, dst_name, mode, uid, gid, src_flags, dst_flags)
176 off_t libreport_copy_file(const char *src_name, const char *dst_name, int mode);
177 off_t libreport_copy_file_at(const char *src_name, int dir_fd, const char *name, int mode);
178 int libreport_copy_file_recursive(const char *source, const char *dest);
179 
180 int libreport_decompress_fd(int fdi, int fdo);
181 int libreport_decompress_file(const char *path_in, const char *path_out, mode_t mode_out);
182 int libreport_decompress_file_ext_at(const char *path_in, int dir_fd, const char *path_out,
183  mode_t mode_out, uid_t uid, gid_t gid, int src_flags, int dst_flags);
184 
185 // NB: will return short read on error, not -1,
186 // if some data was read before error occurred
187 void libreport_xread(int fd, void *buf, size_t count);
188 ssize_t libreport_safe_read(int fd, void *buf, size_t count);
189 ssize_t libreport_safe_write(int fd, const void *buf, size_t count);
190 ssize_t libreport_full_read(int fd, void *buf, size_t count);
191 ssize_t libreport_full_write(int fd, const void *buf, size_t count);
192 ssize_t libreport_full_write_str(int fd, const char *buf);
193 void *libreport_xmalloc_read(int fd, size_t *maxsz_p);
194 void *libreport_xmalloc_open_read_close(const char *filename, size_t *maxsz_p);
195 void *libreport_xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p);
196 char *libreport_malloc_readlink(const char *linkname);
197 char *libreport_malloc_readlinkat(int dir_fd, const char *linkname);
198 
199 
200 /* Returns malloc'ed block */
201 char *libreport_encode_base64(const void *src, int length);
202 
203 /* Returns NULL if the string needs no sanitizing.
204  * control_chars_to_sanitize is a bit mask.
205  * If Nth bit is set, Nth control char will be sanitized (replaced by [XX]).
206  */
207 char *libreport_sanitize_utf8(const char *src, uint32_t control_chars_to_sanitize);
208 enum {
209  SANITIZE_ALL = 0xffffffff,
210  SANITIZE_TAB = (1 << 9),
211  SANITIZE_LF = (1 << 10),
212  SANITIZE_CR = (1 << 13),
213 };
214 
215 int libreport_try_atou(const char *numstr, unsigned *value);
216 unsigned libreport_xatou(const char *numstr);
217 int libreport_try_atoi(const char *numstr, int *value);
218 int libreport_xatoi(const char *numstr);
219 /* Using libreport_xatoi() instead of naive atoi() is not always convenient -
220  * in many places people want *non-negative* values, but store them
221  * in signed int. Therefore we need this one:
222  * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc.
223  * It should really be named xatoi_nonnegative (since it allows 0),
224  * but that would be too long.
225  */
226 int libreport_try_atoi_positive(const char *numstr, int *value);
227 int libreport_xatoi_positive(const char *numstr);
228 
229 //unused for now
230 //unsigned long long monotonic_ns(void);
231 //unsigned long long monotonic_us(void);
232 //unsigned monotonic_sec(void);
233 
234 pid_t libreport_safe_waitpid(pid_t pid, int *wstat, int options);
235 
236 enum {
237  /* on return, pipefds[1] is fd to which parent may write
238  * and deliver data to child's stdin: */
239  EXECFLG_INPUT = 1 << 0,
240  /* on return, pipefds[0] is fd from which parent may read
241  * child's stdout: */
242  EXECFLG_OUTPUT = 1 << 1,
243  /* open child's stdin to /dev/null: */
244  EXECFLG_INPUT_NUL = 1 << 2,
245  /* open child's stdout to /dev/null: */
246  EXECFLG_OUTPUT_NUL = 1 << 3,
247  /* redirect child's stderr to stdout: */
248  EXECFLG_ERR2OUT = 1 << 4,
249  /* open child's stderr to /dev/null: */
250  EXECFLG_ERR_NUL = 1 << 5,
251  /* suppress perror_msg("Can't execute 'foo'") if exec fails */
252  EXECFLG_QUIET = 1 << 6,
253  EXECFLG_SETGUID = 1 << 7,
254  EXECFLG_SETSID = 1 << 8,
255  EXECFLG_SETPGID = 1 << 9,
256 };
257 /*
258  * env_vec: list of variables to set in environment (if string has
259  * "VAR=VAL" form) or unset in environment (if string has no '=' char).
260  *
261  * Returns pid.
262  */
263 pid_t libreport_fork_execv_on_steroids(int flags,
264  char **argv,
265  int *pipefds,
266  char **env_vec,
267  const char *dir,
268  uid_t uid);
269 /* Returns malloc'ed string. NULs are retained, and extra one is appended
270  * after the last byte (this NUL is not accounted for in *size_p) */
271 char *libreport_run_in_shell_and_save_output(int flags,
272  const char *cmd,
273  const char *dir,
274  size_t *size_p);
275 
276 /* Random utility functions */
277 
278 bool libreport_is_in_string_list(const char *name, const char *const *v);
279 
280 int libreport_index_of_string_in_list(const char *name, const char *const *v);
281 
282 bool libreport_is_in_comma_separated_list(const char *value, const char *list);
283 bool libreport_is_in_comma_separated_list_of_glob_patterns(const char *value, const char *list);
284 
285 /* Calls GLib version appropriate initialization function.
286  */
287 void libreport_glib_init(void);
288 
289 /* Frees every element'd data using free(),
290  * then frees list itself using g_list_free(list):
291  */
292 void libreport_list_free_with_free(GList *list);
293 
294 double libreport_get_dirsize(const char *pPath);
295 double libreport_get_dirsize_find_largest_dir(
296  const char *pPath,
297  char **worst_dir, /* can be NULL */
298  const char *excluded /* can be NULL */
299 );
300 
301 int libreport_ndelay_on(int fd);
302 int libreport_ndelay_off(int fd);
303 int libreport_close_on_exec_on(int fd);
304 
305 void *libreport_xmalloc(size_t size);
306 void *libreport_xrealloc(void *ptr, size_t size);
307 void *libreport_xzalloc(size_t size);
308 char *libreport_xstrdup(const char *s);
309 char *libreport_xstrndup(const char *s, int n);
310 char *libreport_xstrdup_between(const char *s, const char *open, const char *close);
311 
312 void libreport_xpipe(int filedes[2]);
313 int libreport_xdup(int from);
314 void libreport_xdup2(int from, int to);
315 void libreport_xmove_fd(int from, int to);
316 
317 void libreport_xwrite(int fd, const void *buf, size_t count);
318 void libreport_xwrite_str(int fd, const char *str);
319 
320 off_t libreport_xlseek(int fd, off_t offset, int whence);
321 
322 void libreport_xchdir(const char *path);
323 
324 char *libreport_xvasprintf(const char *format, va_list p);
325 char *libreport_xasprintf(const char *format, ...);
326 
327 void libreport_xsetenv(const char *key, const char *value);
328 /*
329  * Utility function to unsetenv a string which was possibly putenv'ed.
330  * The problem here is that "natural" optimization:
331  * strchrnul(var_val, '=')[0] = '\0';
332  * unsetenv(var_val);
333  * is BUGGY: if string was put into environment via putenv,
334  * its modification (s/=/NUL/) is illegal, and unsetenv will fail to unset it.
335  * Of course, saving/restoring the char wouldn't work either.
336  * This helper creates a copy up to '=', unsetenv's it, and frees:
337  */
338 void libreport_safe_unsetenv(const char *var_val);
339 
340 int libreport_xsocket(int domain, int type, int protocol);
341 void libreport_xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
342 void libreport_xlisten(int s, int backlog);
343 ssize_t libreport_xsendto(int s, const void *buf, size_t len,
344  const struct sockaddr *to, socklen_t tolen);
345 
346 void libreport_xstat(const char *name, struct stat *stat_buf);
347 off_t libreport_fstat_st_size_or_die(int fd);
348 off_t libreport_stat_st_size_or_die(const char *filename);
349 
350 int libreport_xopen3(const char *pathname, int flags, int mode);
351 int libreport_xopen(const char *pathname, int flags);
352 void libreport_xunlink(const char *pathname);
353 void libreport_xunlinkat(int dir_fd, const char *pathname, int flags);
354 
355 /* Just testing dent->d_type == DT_REG is wrong: some filesystems
356  * do not report the type, they report DT_UNKNOWN for every dirent
357  * (and this is not a bug in filesystem, this is allowed by standards).
358  * This function handles this case. Note: it returns 0 on symlinks
359  * even if they point to regular files.
360  */
361 int libreport_is_regular_file(struct dirent *dent, const char *dirname);
362 int libreport_is_regular_file_at(struct dirent *dent, int dir_fd);
363 
364 bool libreport_dot_or_dotdot(const char *filename);
365 char *libreport_last_char_is(const char *s, int c);
366 
367 bool libreport_string_to_bool(const char *s);
368 
369 void libreport_xseteuid(uid_t euid);
370 void libreport_xsetegid(gid_t egid);
371 void libreport_xsetreuid(uid_t ruid, uid_t euid);
372 void libreport_xsetregid(gid_t rgid, gid_t egid);
373 
374 FILE *libreport_xfdopen(int fd, const char *mode);
375 
376 /* Emit a string of hex representation of bytes */
377 char *libreport_bin2hex(char *dst, const char *str, int count);
378 /* Convert "xxxxxxxx" hex string to binary, no more than COUNT bytes */
379 char* libreport_hex2bin(char *dst, const char *str, int count);
380 
381 
382 enum {
383  LOGMODE_NONE = 0,
384  LOGMODE_STDIO = (1 << 0),
385  LOGMODE_SYSLOG = (1 << 1),
386  LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
387  LOGMODE_CUSTOM = (1 << 2),
388  LOGMODE_JOURNAL = (1 << 3),
389 };
390 
391 enum libreport_diemode {
392  DIEMODE_EXIT = 0,
393  DIEMODE_ABORT = 1,
394 };
395 
396 extern void (*libreport_g_custom_logger)(const char*);
397 extern const char *libreport_msg_prefix;
398 extern const char *libreport_msg_eol;
399 extern int libreport_logmode;
400 extern int libreport_xfunc_error_retval;
401 
402 /* A few magic exit codes */
403 #define EXIT_CANCEL_BY_USER 69
404 #define EXIT_STOP_EVENT_RUN 70
405 
406 void libreport_set_xfunc_error_retval(int retval);
407 
408 void libreport_set_xfunc_diemode(enum libreport_diemode mode);
409 
410 /* Verbosity level */
411 extern int libreport_g_verbose;
412 /* VERB1 log_warning("what you sometimes want to see, even on a production box") */
413 #define VERB1 if (libreport_g_verbose >= 1)
414 /* VERB2 log_warning("debug message, not going into insanely small details") */
415 #define VERB2 if (libreport_g_verbose >= 2)
416 /* VERB3 log_warning("lots and lots of details") */
417 #define VERB3 if (libreport_g_verbose >= 3)
418 /* there is no level > 3 */
419 
420 #define libreport_
421 void libreport_xfunc_die(void) NORETURN;
422 
423 void libreport_die_out_of_memory(void) NORETURN;
424 
425 /* It's a macro, not function, since it collides with log_warning() from math.h */
426 #undef log
427 #define log_warning(...) log_standard(LOG_WARNING, __FILE__, __LINE__, __func__, __VA_ARGS__)
428 #define log_debug(...) log_standard(LOG_DEBUG, __FILE__, __LINE__, __func__, __VA_ARGS__)
429 #define log_info(...) log_standard(LOG_INFO, __FILE__, __LINE__, __func__, __VA_ARGS__)
430 #define log_notice(...) log_standard(LOG_NOTICE, __FILE__, __LINE__, __func__, __VA_ARGS__)
431 #define log_warning(...) log_standard(LOG_WARNING, __FILE__, __LINE__, __func__, __VA_ARGS__)
432 #define log_error(...) log_standard(LOG_ERR, __FILE__, __LINE__, __func__, __VA_ARGS__)
433 
434 // specific subsystem debugging
435 #define log_parser(...) if(0) log_debug(__VA_ARGS__)
436 
437 #define log_standard(level, file, line, func, ...) log_wrapper(level, __FILE__, __LINE__, __func__, false, false, __VA_ARGS__)
438 
439 // level, file, line, func, perror, custom logger, format & args
440 #define log_error_and_die(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, false, false,__VA_ARGS__)
441 #define log_perror(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, false, __VA_ARGS__)
442 #define log_perror_and_die(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, false, __VA_ARGS__)
443 
444 #define error_msg(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
445 #define perror_msg(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
446 #define warn_msg(...) log_wrapper(LOG_WARNING, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
447 #define pwarn_msg(...) log_wrapper(LOG_WARNING, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
448 #define notice_msg(...) log_wrapper(LOG_NOTICE, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
449 #define pnotice_msg(...) log_wrapper(LOG_NOTICE, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
450 #define error_msg_and_die(...) log_and_die_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
451 #define perror_msg_and_die(...) log_and_die_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
452 
453 
454 void log_wrapper(int level,
455  const char *file,
456  int line,
457  const char *func,
458  bool process_perror,
459  bool use_custom_logger,
460  const char *format, ...) __attribute__ ((format (printf, 7,8)));
461 
462 void log_and_die_wrapper(int level,
463  const char *file,
464  int line,
465  const char *func,
466  bool process_perror,
467  bool use_custom_logger,
468  const char *format, ...) __attribute__ ((noreturn, format (printf, 7,8)));
469 
470 struct strbuf
471 {
472  /* Size of the allocated buffer. Always > 0. */
473  int alloc;
474  /* Length of the string, without the ending \0. */
475  int len;
476  char *buf;
477 };
478 
485 struct strbuf *libreport_strbuf_new(void);
486 
492 void libreport_strbuf_free(struct strbuf *strbuf);
493 
499 char *libreport_strbuf_free_nobuf(struct strbuf *strbuf);
500 
505 void libreport_strbuf_clear(struct strbuf *strbuf);
506 
511 struct strbuf *libreport_strbuf_append_char(struct strbuf *strbuf, char c);
512 
517 struct strbuf *libreport_strbuf_append_str(struct strbuf *strbuf,
518  const char *str);
519 
524 struct strbuf *libreport_strbuf_prepend_str(struct strbuf *strbuf,
525  const char *str);
526 
531 struct strbuf *libreport_strbuf_append_strf(struct strbuf *strbuf,
532  const char *format, ...);
533 
538 struct strbuf *libreport_strbuf_append_strfv(struct strbuf *strbuf,
539  const char *format, va_list p);
540 
546 struct strbuf *libreport_strbuf_prepend_strf(struct strbuf *strbuf,
547  const char *format, ...);
548 
553 struct strbuf *libreport_strbuf_prepend_strfv(struct strbuf *strbuf,
554  const char *format, va_list p);
555 
556 /* Returns command line of running program.
557  * Caller is responsible to free() the returned value.
558  * If the pid is not valid or command line can not be obtained,
559  * empty string is returned.
560  */
561 int libreport_open_proc_pid_dir(pid_t pid);
562 char *libreport_get_cmdline_at(pid_t pid);
563 char *libreport_get_cmdline(pid_t pid);
564 char *libreport_get_environ_at(pid_t pid);
565 char *libreport_get_environ(pid_t pid);
566 char *libreport_get_executable_at(pid_t pid);
567 char *libreport_get_executable(pid_t pid);
568 char *libreport_get_cwd_at(pid_t pid);
569 char *libreport_get_cwd(pid_t pid);
570 char *libreport_get_rootdir_at(pid_t pid);
571 char *libreport_get_rootdir(pid_t pid);
572 
573 int libreport_get_fsuid(const char *proc_pid_status);
574 int libreport_get_fsgid(const char *proc_pid_status);
575 int libreport_dump_fd_info_at(int pid_proc_fd, FILE *dest);
576 int libreport_dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid);
577 int libreport_dump_fd_info(const char *dest_filename, const char *proc_pid_fd_path);
578 int libreport_get_env_variable_ext(int fd, char delim, const char *name, char **value);
579 int libreport_get_env_variable(pid_t pid, const char *name, char **value);
580 
581 #define PROC_NS_UNSUPPORTED ((ino_t)-1)
582 #define PROC_NS_ID_CGROUP 0
583 #define PROC_NS_ID_IPC 1
584 #define PROC_NS_ID_MNT 2
585 #define PROC_NS_ID_NET 3
586 #define PROC_NS_ID_PID 4
587 #define PROC_NS_ID_TIME 6
588 #define PROC_NS_ID_USER 8
589 #define PROC_NS_ID_UTS 9
590 static const char * libreport_proc_namespaces[] = {
591  "cgroup",
592  "ipc",
593  "mnt",
594  "net",
595  "pid",
596  "pid_for_children",
597  "time",
598  "time_for_children",
599  "user",
600  "uts",
601 };
602 
603 struct ns_ids {
604  ino_t nsi_ids[ARRAY_SIZE(libreport_proc_namespaces)];
605 };
606 
607 int libreport_get_ns_ids_at(int pid_proc_fd, struct ns_ids *ids);
608 int libreport_get_ns_ids(pid_t pid, struct ns_ids *ids);
609 
610 /* These functions require a privileged user and does not work correctly in
611  * processes running in own PID namespace
612  */
613 int libreport_process_has_own_root_at(int proc_pid_fd);
614 int libreport_process_has_own_root(pid_t pid);
615 
616 int libreport_get_pid_of_container_at(int pid_proc_fd, pid_t *init_pid);
617 int libreport_get_pid_of_container(pid_t pid, pid_t *init_pid);
618 int libreport_dump_namespace_diff_at(int base_pid_proc_fd, int tested_pid_proc_fd, FILE *dest);
619 int libreport_dump_namespace_diff_ext(const char *dest_filename, pid_t base_pid, pid_t tested_pid, uid_t uid, gid_t gid);
620 int libreport_dump_namespace_diff(const char *dest_filename, pid_t base_pid, pid_t tested_pid);
621 
622 enum
623 {
624  MOUNTINFO_INDEX_MOUNT_ID,
625  MOUNTINFO_INDEX_PARENT_ID,
626  MOUNTINFO_INDEX_MAJOR_MINOR,
627  MOUNTINFO_INDEX_ROOT,
628  MOUNTINFO_INDEX_MOUNT_POINT,
629  MOUNTINFO_INDEX_MOUNT_OPTIONS,
630  MOUNTINFO_INDEX_OPTIONAL_FIELDS,
631  MOUNTINFO_INDEX_FS_TYPE,
632  MOUNTINFO_INDEX_MOUNT_SOURCE,
633  MOUNTINFO_INDEX_SUPER_OPITONS,
634  _MOUNTINFO_INDEX_MAX,
635 };
636 
637 #define MOUNTINFO_ROOT(val) (val.mntnf_items[MOUNTINFO_INDEX_ROOT])
638 #define MOUNTINFO_MOUNT_POINT(val) (val.mntnf_items[MOUNTINFO_INDEX_MOUNT_POINT])
639 #define MOUNTINFO_MOUNT_SOURCE(val) (val.mntnf_items[MOUNTINFO_INDEX_MOUNT_SOURCE])
640 
641 struct mountinfo
642 {
643  /* 4 : root of the mount within the filesystem */
644  /* 5 : mount point relative to the process's root */
645  /* 10 : mount source: filesystem specific information or "none" */
646  /* but it mount source is preceded by 0 or more optional fields */
647  /* so the effective value is 9 */
648  char *mntnf_items[_MOUNTINFO_INDEX_MAX];
649 };
650 void libreport_mountinfo_destroy(struct mountinfo *mntnf);
651 int libreport_get_mountinfo_for_mount_point(FILE *fin, struct mountinfo *mntnf, const char *mnt_point);
652 
653 /* Takes ptr to time_t, or NULL if you want to use current time.
654  * Returns "YYYY-MM-DD-hh:mm:ss" string.
655  */
656 char *libreport_iso_date_string(const time_t *pt);
657 #define LIBREPORT_ISO_DATE_STRING_SAMPLE "YYYY-MM-DD-hh:mm:ss"
658 #define LIBREPORT_ISO_DATE_STRING_FORMAT "%Y-%m-%d-%H:%M:%S"
659 
660 /* Parses date into integer UNIX time stamp
661  *
662  * @param date The parsed date string
663  * @param pt Return value
664  * @return 0 on success; otherwise non-0 number. -EINVAL if the parameter date
665  * does not match LIBREPORT_ISO_DATE_STRING_FORMAT
666  */
667 int libreport_iso_date_string_parse(const char *date, time_t *pt);
668 
669 enum {
670  MAKEDESC_SHOW_FILES = (1 << 0),
671  MAKEDESC_SHOW_MULTILINE = (1 << 1),
672  MAKEDESC_SHOW_ONLY_LIST = (1 << 2),
673  MAKEDESC_WHITELIST = (1 << 3),
674  /* Include all URLs from FILENAME_REPORTED_TO element in the description text */
675  MAKEDESC_SHOW_URLS = (1 << 4),
676 };
677 char *libreport_make_description(problem_data_t *problem_data, char **names_to_skip, unsigned max_text_size, unsigned desc_flags);
678 char *libreport_make_description_logger(problem_data_t *problem_data, unsigned max_text_size);
679 
680 /* See man os-release(5) for details */
681 #define OSINFO_ID "ID"
682 #define OSINFO_NAME "NAME"
683 #define OSINFO_VERSION_ID "VERSION_ID"
684 #define OSINFO_PRETTY_NAME "PRETTY_NAME"
685 
686 /* @brief Loads a text in format of os-release(5) in to a map
687  *
688  * Function doesn't check for format errors much. It just tries to avoid
689  * program errors. In case of error the function prints out a log message and
690  * continues in parsing.
691  *
692  * @param osinfo_bytes Non-NULL pointer to osinfo bytes.
693  * @param osinfo The map where result is stored
694  */
695 void libreport_parse_osinfo(const char *osinfo_bytes, map_string_t *osinfo);
696 
697 /* @brief Builds product string and product's version string for Bugzilla
698  *
699  * At first tries to get strings from the os specific variables
700  * (REDHAT_BUGZILLA_PRODUCT, REDHAT_BUGZILLA_PRODUCT_VERSION) if no such
701  * variables are found, uses NAME key for the product and VERSION_ID key for
702  * the product's version. If neither NAME nor VERSION_ID are provided fallbacks
703  * to parsing of os_release which should be stored under PRETTY_NAME key.
704  *
705  * https://bugzilla.redhat.com/show_bug.cgi?id=950373
706  *
707  * @param osinfo Input data from which the values are built
708  * @param produc Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
709  * @param version Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
710  */
711 void libreport_parse_osinfo_for_bz(map_string_t *osinfo, char **product, char **version);
712 
713 /* @brief Extract BUG_REPORT_URL from os-release
714  *
715  * A default location for bug reports can be stored in os-release.
716  * This extracts the value if present and stores it in url.
717  * If unset, url will become NULL
718  *
719  * https://github.com/abrt/libreport/issues/459
720  *
721  * @param osinfo Input data from which the values are built
722  * @param url Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
723  */
724 void libreport_parse_osinfo_for_bug_url(map_string_t *osinfo, char** url);
725 
726 /* @brief Builds product string and product's version string for Red Hat Support
727  *
728  * At first tries to get strings from the os specific variables
729  * (REDHAT_SUPPORT_PRODUCT, REDHAT_SUPPORT_PRODUCT_VERSION) if no such
730  * variables are found, uses NAME key for the product and VERSION_ID key for
731  * the product's version. If no NAME nor VERSION_ID are provided fallbacks to
732  * parsing of os_release which should be stored under PRETTY_NAME key.
733  *
734  * https://bugzilla.redhat.com/show_bug.cgi?id=950373
735  *
736  * @param osinfo Input data from which the values are built
737  * @param produc Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
738  * @param version Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
739  */
740 void libreport_parse_osinfo_for_rhts(map_string_t *osinfo, char **product, char **version);
741 
742 void libreport_parse_release_for_bz(const char *pRelease, char **product, char **version);
743 void libreport_parse_release_for_rhts(const char *pRelease, char **product, char **version);
744 
759 bool libreport_load_conf_file(const char *pPath, map_string_t *settings, bool skipKeysWithoutValue);
760 bool libreport_load_plugin_conf_file(const char *name, map_string_t *settings, bool skipKeysWithoutValue);
761 
762 const char *libreport_get_user_conf_base_dir(void);
763 
764 bool libreport_load_conf_file_from_dirs(const char *base_name, const char *const *directories, map_string_t *settings, bool skipKeysWithoutValue);
765 
766 enum {
767  CONF_DIR_FLAG_NONE = 0,
768  CONF_DIR_FLAG_OPTIONAL = 1,
769 };
770 
771 bool libreport_load_conf_file_from_dirs_ext(const char *base_name, const char *const *directories,
772  const int * dir_flags, map_string_t *settings,
773  bool skipKeysWithoutValue);
774 
775 bool libreport_save_conf_file(const char *path, map_string_t *settings);
776 bool libreport_save_plugin_conf_file(const char *name, map_string_t *settings);
777 
778 bool libreport_save_app_conf_file(const char* application_name, map_string_t *settings);
779 bool libreport_load_app_conf_file(const char *application_name, map_string_t *settings);
780 void libreport_set_app_user_setting(map_string_t *settings, const char *name, const char *value);
781 const char *libreport_get_app_user_setting(map_string_t *settings, const char *name);
782 
783 bool libreport_save_user_settings(void);
784 bool libreport_load_user_settings(const char *application_name);
785 void libreport_set_user_setting(const char *name, const char *value);
786 const char *libreport_get_user_setting(const char *name);
787 
788 /* filename is expected to exist in CONF_DIR
789  * usually /etc/libreport
790  */
791 GList *libreport_load_words_from_file(const char *filename);
792 GList *libreport_get_file_list(const char *path, const char *ext);
793 void libreport_free_file_list(GList *filelist);
794 file_obj_t *libreport_new_file_obj(const char* fullpath, const char* filename);
795 void libreport_free_file_obj(file_obj_t *f);
796 GList *libreport_parse_delimited_list(const char *string, const char *delimiter);
797 
798 /* Connect to abrtd over unix domain socket, issue DELETE command */
799 int delete_dump_dir_possibly_using_abrtd(const char *dump_dir_name);
800 
801 /* Tries to create a copy of dump_dir_name in base_dir, with same or similar basename.
802  * Returns NULL if copying failed. In this case, logs a message before returning. */
803 struct dump_dir *libreport_steal_directory(const char *base_dir, const char *dump_dir_name);
804 
805 /* Resolves if the given user is in given group
806  *
807  * @param uid user ID
808  * @param gid group ID
809  * @returns TRUE in case the user is in the group otherwise returns FALSE
810  */
811 bool libreport_uid_in_group(uid_t uid, gid_t gid);
812 
813 /* Tries to open dump_dir_name with writing access. If function needs to steal
814  * directory calls ask_continue(new base dir, dump dir) callback to ask user
815  * for permission. If ask_continue param is NULL the function thinks that an
816  * answer is positive and steals directory.
817  * Returns NULL if opening failed or if stealing was dismissed. In this case,
818  * logs a message before returning. */
819 struct dump_dir *libreport_open_directory_for_writing(
820  const char *dump_dir_name,
821  bool (*ask_continue)(const char *, const char *));
822 
823 // Files bigger than this are never considered to be text.
824 //
825 // Started at 64k limit. But _some_ limit is necessary:
826 // fields declared "text" may end up in editing fields and such.
827 // We don't want to accidentally end up with 100meg text in a textbox!
828 // So, don't remove this. If you really need to, raise the limit.
829 //
830 // Bumped up to 200k: saw 124740 byte /proc/PID/smaps file
831 // Bumped up to 500k: saw 375252 byte anaconda traceback file
832 // Bumped up to 1M: bugzilla.redhat.com/show_bug.cgi?id=746727
833 // mentions 853646 byte anaconda-tb-* file.
834 // Bumped up to 8M: bugzilla.redhat.com/show_bug.cgi?id=887570
835 // (anaconda-tb file of 1.38 MBytes)
836 //
837 #define CD_MAX_TEXT_SIZE (8*1024*1024)
838 
839 // Text bigger than this usually is attached, not added inline
840 // was 2k, 20kb is too much, let's try 4kb
841 //
842 // For bug databases
843 #define CD_TEXT_ATT_SIZE_BZ (4*1024)
844 // For dumping problem data into a text file, email, etc
845 #define CD_TEXT_ATT_SIZE_LOGGER (CD_MAX_TEXT_SIZE)
846 
847 // Filenames in problem directory:
848 // filled by a hook:
849 #define FILENAME_TIME "time" /* mandatory */
850 #define FILENAME_LAST_OCCURRENCE "last_occurrence" /* optional */
851 #define FILENAME_REASON "reason" /* mandatory? */
852 #define FILENAME_UID "uid" /* mandatory? */
853 
854 /*
855  * "analyzer" is to be gradually changed to "type":
856  * For now, we fetch and look at "analyzer" element,
857  * but we always save both "analyzer" and "type" (with same contents).
858  * By 2013, we switch to looking at "type". Then we will stop generating
859  * "analyzer" element.
860  * ----
861  * Update 2015: based on the recent changes where we have introduced several
862  * tools generating one problem type, we have decided to retain 'analyzer'
863  * file, but it shall contain string identifier of a tool that created the
864  * problem.
865  */
866 #define FILENAME_ANALYZER "analyzer"
867 #define FILENAME_TYPE "type"
868 #define FILENAME_EXECUTABLE "executable"
869 #define FILENAME_PID "pid"
870 #define FILENAME_TID "tid"
871 #define FILENAME_GLOBAL_PID "global_pid"
872 #define FILENAME_PWD "pwd"
873 #define FILENAME_ROOTDIR "rootdir"
874 #define FILENAME_BINARY "binary"
875 #define FILENAME_CMDLINE "cmdline"
876 #define FILENAME_COREDUMP "coredump"
877 #define FILENAME_CGROUP "cgroup"
878 #define FILENAME_BACKTRACE "backtrace"
879 #define FILENAME_MAPS "maps"
880 #define FILENAME_SMAPS "smaps"
881 #define FILENAME_PROC_PID_STATUS "proc_pid_status"
882 #define FILENAME_ENVIRON "environ"
883 #define FILENAME_LIMITS "limits"
884 #define FILENAME_OPEN_FDS "open_fds"
885 #define FILENAME_MOUNTINFO "mountinfo"
886 #define FILENAME_NAMESPACES "namespaces"
887 #define FILENAME_CPUINFO "cpuinfo"
888 
889 /* Global problem identifier which is usually generated by some "analyze_*"
890  * event because it may take a lot of time to obtain strong problem
891  * identification */
892 #define FILENAME_DUPHASH "duphash"
893 
894 // Name of the function where the application crashed.
895 // Optional.
896 #define FILENAME_CRASH_FUNCTION "crash_function"
897 #define FILENAME_ARCHITECTURE "architecture"
898 #define FILENAME_KERNEL "kernel"
899 /*
900  * From /etc/os-release
901  * os_release filename name is alredy occupied by /etc/redhat-release (see
902  * below) in sake of backward compatibility /etc/os-release is stored in
903  * os_info file
904  */
905 #define FILENAME_OS_INFO "os_info"
906 #define FILENAME_OS_INFO_IN_ROOTDIR "os_info_in_rootdir"
907 // From /etc/system-release or /etc/redhat-release
908 #define FILENAME_OS_RELEASE "os_release"
909 #define FILENAME_OS_RELEASE_IN_ROOTDIR "os_release_in_rootdir"
910 // Filled by <what?>
911 #define FILENAME_PACKAGE "package"
912 #define FILENAME_COMPONENT "component"
913 #define FILENAME_COMMENT "comment"
914 #define FILENAME_RATING "backtrace_rating"
915 #define FILENAME_HOSTNAME "hostname"
916 // Optional. Set to "1" by abrt-handle-upload for every unpacked dump
917 #define FILENAME_REMOTE "remote"
918 #define FILENAME_TAINTED "kernel_tainted"
919 #define FILENAME_TAINTED_SHORT "kernel_tainted_short"
920 #define FILENAME_TAINTED_LONG "kernel_tainted_long"
921 #define FILENAME_VMCORE "vmcore"
922 #define FILENAME_KERNEL_LOG "kernel_log"
923 // File created by createAlertSignature() from libreport's python module
924 // The file should contain a description of an alert
925 #define FILENAME_DESCRIPTION "description"
926 
927 /* Local problem identifier (weaker than global identifier) designed for fast
928  * local for fast local duplicate identification. This file is usually provided
929  * by crashed application (problem creator).
930  */
931 #define FILENAME_UUID "uuid"
932 
933 #define FILENAME_COUNT "count"
934 /* Multi-line list of places problem was reported.
935  * Recommended line format:
936  * "Reporter: VAR=VAL VAR=VAL"
937  * Use libreport_add_reported_to(dd, "line_without_newline"): it adds line
938  * only if it is not already there.
939  */
940 #define FILENAME_REPORTED_TO "reported_to"
941 #define FILENAME_EVENT_LOG "event_log"
942 /*
943  * If exists, should contain a full sentence (with trailing period)
944  * which describes why this problem should not be reported.
945  * Example: "Your laptop firmware 1.9a is buggy, version 1.10 contains the fix."
946  */
947 #define FILENAME_NOT_REPORTABLE "not-reportable"
948 #define FILENAME_CORE_BACKTRACE "core_backtrace"
949 #define FILENAME_REMOTE_RESULT "remote_result"
950 #define FILENAME_PKG_EPOCH "pkg_epoch"
951 #define FILENAME_PKG_NAME "pkg_name"
952 #define FILENAME_PKG_VERSION "pkg_version"
953 #define FILENAME_PKG_RELEASE "pkg_release"
954 #define FILENAME_PKG_ARCH "pkg_arch"
955 
956 /* RHEL packages - Red Hat, Inc. */
957 #define FILENAME_PKG_VENDOR "pkg_vendor"
958 /* RHEL keys - https://access.redhat.com/security/team/key */
959 #define FILENAME_PKG_FINGERPRINT "pkg_fingerprint"
960 
961 #define FILENAME_USERNAME "username"
962 #define FILENAME_ABRT_VERSION "abrt_version"
963 #define FILENAME_EXPLOITABLE "exploitable"
964 
965 /* reproducible element is used by functions from problem_data.h */
966 #define FILENAME_REPRODUCIBLE "reproducible"
967 #define FILENAME_REPRODUCER "reproducer"
968 
969 /* File names related to Anaconda problems
970  */
971 #define FILENAME_KICKSTART_CFG "ks.cfg"
972 #define FILENAME_ANACONDA_TB "anaconda-tb"
973 
974 /* Containers
975  */
976 #define FILENAME_CONTAINER "container"
977 #define FILENAME_CONTAINER_ID "container_id"
978 #define FILENAME_CONTAINER_UUID "container_uuid"
979 #define FILENAME_CONTAINER_IMAGE "container_image"
980 #define FILENAME_CONTAINER_CMDLINE "container_cmdline"
981 /* Container root file-system directory as seen from the host. */
982 #define FILENAME_CONTAINER_ROOTFS "container_rootfs"
983 #define FILENAME_DOCKER_INSPECT "docker_inspect"
984 
985 /* Type of catched exception
986  * Optional.
987  */
988 #define FILENAME_EXCEPTION_TYPE "exception_type"
989 
990 // Not stored as files, added "on the fly":
991 #define CD_DUMPDIR "Directory"
992 
993 gint libreport_cmp_problem_data(gconstpointer a, gconstpointer b, gpointer filename);
994 
995 //UNUSED:
998 //#define CD_EVENTS "Events"
999 
1000 /* FILENAME_EVENT_LOG is trimmed to below LOW_WATERMARK
1001  * when it reaches HIGH_WATERMARK size
1002  */
1003 enum {
1004  EVENT_LOG_HIGH_WATERMARK = 30 * 1024,
1005  EVENT_LOG_LOW_WATERMARK = 20 * 1024,
1006 };
1007 
1008 void libreport_log_problem_data(problem_data_t *problem_data, const char *pfx);
1009 
1010 extern int g_libreport_inited;
1011 void libreport_init(void);
1012 
1013 #define INITIALIZE_LIBREPORT() \
1014  do \
1015  { \
1016  if (!g_libreport_inited) \
1017  { \
1018  g_libreport_inited = 1; \
1019  libreport_init(); \
1020  } \
1021  } \
1022  while (0)
1023 
1024 const char *abrt_init(char **argv);
1025 void libreport_export_abrt_envvars(int pfx);
1026 extern const char *libreport_g_progname;
1027 
1028 enum parse_opt_type {
1029  OPTION_BOOL,
1030  OPTION_GROUP,
1031  OPTION_STRING,
1032  OPTION_INTEGER,
1033  OPTION_OPTSTRING,
1034  OPTION_LIST,
1035  OPTION_END,
1036 };
1037 
1038 struct options {
1039  enum parse_opt_type type;
1040  int short_name;
1041  const char *long_name;
1042  void *value;
1043  const char *argh;
1044  const char *help;
1045 };
1046 
1047 /*
1048  * s - short_name
1049  * l - long_name
1050  * v - value
1051  * a - option parameter name (for help text)
1052  * h - help
1053  */
1054 #define OPT_END() { OPTION_END }
1055 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
1056 #define OPT_BOOL( s, l, v, h) { OPTION_BOOL , (s), (l), (v), NULL , (h) }
1057 #define OPT_INTEGER( s, l, v, h) { OPTION_INTEGER , (s), (l), (v), "NUM", (h) }
1058 #define OPT_STRING( s, l, v, a, h) { OPTION_STRING , (s), (l), (v), (a) , (h) }
1059 #define OPT_OPTSTRING(s, l, v, a, h) { OPTION_OPTSTRING, (s), (l), (v), (a) , (h) }
1060 #define OPT_LIST( s, l, v, a, h) { OPTION_LIST , (s), (l), (v), (a) , (h) }
1061 
1062 #define OPT__VERBOSE(v) OPT_BOOL('v', "verbose", (v), _("Be verbose"))
1063 #define OPT__DUMP_DIR(v) OPT_STRING('d', "problem-dir", (v), "DIR", _("Problem directory"))
1064 
1065 unsigned libreport_parse_opts(int argc, char **argv, const struct options *opt,
1066  const char *usage);
1067 
1068 void libreport_show_usage_and_die(const char *usage, const struct options *opt) NORETURN;
1069 
1070 /* Can't include "abrt_curl.h", it's not a public API.
1071  * Resorting to just forward-declaring the struct we need.
1072  */
1073 struct abrt_post_state;
1074 
1075 /* Decomposes uri to its base elements, removes userinfo out of the hostname and
1076  * composes a new uri without userinfo.
1077  *
1078  * The function does not validate the url.
1079  *
1080  * @param uri The uri that might contain userinfo
1081  * @param result The userinfo free uri will be store here. Cannot be null. Must
1082  * be de-allocated by free.
1083  * @param scheme Scheme of the uri. Can be NULL. Result can be NULL. Result
1084  * must be de-allocated by free.
1085  * @param hostname Hostname of the uri. Can be NULL. Result can be NULL. Result
1086  * must be de-allocated by free.
1087  * @param username Username of the uri. Can be NULL. Result can be NULL. Result
1088  * must be de-allocated by free.
1089  * @param password Password of the uri. Can be NULL. Result can be NULL. Result
1090  * must be de-allocated by free.
1091  * @param location Location of the uri. Can be NULL. Result is never NULL. Result
1092  * must be de-allocated by free.
1093  */
1094 int libreport_uri_userinfo_remove(const char *uri, char **result, char **scheme, char **hostname, char **username, char **password, char **location);
1095 
1096 #ifdef __cplusplus
1097 }
1098 #endif
1099 
1100 #endif
strbuf
Definition: internal_libreport.h:471
problem_data.h
mountinfo
Definition: internal_libreport.h:642
file_obj
Definition: file_obj.h:21
options
Definition: internal_libreport.h:1038
dump_dir
Definition: dump_dir.h:97
ns_ids
Definition: internal_libreport.h:603