libreport  2.3.0
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 #if ENABLE_NLS
63 # include <libintl.h>
64 # define _(S) dgettext(PACKAGE, S)
65 #else
66 # define _(S) (S)
67 #endif
68 
69 #if 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 
91 /* Pull in entire public libreport API */
92 #include "dump_dir.h"
93 #include "event_config.h"
94 #include "problem_data.h"
95 #include "report.h"
96 #include "run_event.h"
97 #include "workflow.h"
98 #include "file_obj.h"
99 #include "libreport_types.h"
100 
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104 
105 #define prefixcmp libreport_prefixcmp
106 int prefixcmp(const char *str, const char *prefix);
107 #define suffixcmp libreport_suffixcmp
108 int suffixcmp(const char *str, const char *suffix);
109 #define strtrim libreport_strtrim
110 char *strtrim(char *str);
111 #define strtrimch libreport_strtrimch
112 char *strtrimch(char *str, int ch);
113 #define strremovech libreport_strremovech
114 char *strremovech(char *str, int ch);
115 #define append_to_malloced_string libreport_append_to_malloced_string
116 char *append_to_malloced_string(char *mstr, const char *append);
117 #define skip_whitespace libreport_skip_whitespace
118 char* skip_whitespace(const char *s);
119 #define skip_non_whitespace libreport_skip_non_whitespace
120 char* skip_non_whitespace(const char *s);
121 /* Like strcpy but can copy overlapping strings. */
122 #define overlapping_strcpy libreport_overlapping_strcpy
123 void overlapping_strcpy(char *dst, const char *src);
124 
125 #define concat_path_file libreport_concat_path_file
126 char *concat_path_file(const char *path, const char *filename);
127 /*
128  * Used to construct a name in a different directory with the basename
129  * similar to the old name, if possible.
130  */
131 #define concat_path_basename libreport_concat_path_basename
132 char *concat_path_basename(const char *path, const char *filename);
133 
134 /* Allows all printable characters except '/',
135  * the string must not exceed 64 characters of length
136  * and must not equal neither "." nor ".." (these strings may appear in the string) */
137 #define str_is_correct_filename libreport_str_is_correct_filename
138 bool str_is_correct_filename(const char *str);
139 
140 /* A-la fgets, but malloced and of unlimited size */
141 #define xmalloc_fgets libreport_xmalloc_fgets
142 char *xmalloc_fgets(FILE *file);
143 /* Similar, but removes trailing \n */
144 #define xmalloc_fgetline libreport_xmalloc_fgetline
145 char *xmalloc_fgetline(FILE *file);
146 /* Useful for easy reading of various /proc files */
147 #define xmalloc_fopen_fgetline_fclose libreport_xmalloc_fopen_fgetline_fclose
148 char *xmalloc_fopen_fgetline_fclose(const char *filename);
149 
150 
151 /* On error, copyfd_XX prints error messages and returns -1 */
152 enum {
153  COPYFD_SPARSE = 1 << 0,
154 };
155 #define copyfd_eof libreport_copyfd_eof
156 off_t copyfd_eof(int src_fd, int dst_fd, int flags);
157 #define copyfd_size libreport_copyfd_size
158 off_t copyfd_size(int src_fd, int dst_fd, off_t size, int flags);
159 #define copyfd_exact_size libreport_copyfd_exact_size
160 void copyfd_exact_size(int src_fd, int dst_fd, off_t size);
161 #define copy_file_ext_at libreport_copy_file_ext_at
162 off_t 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);
163 #define copy_file_ext(src_name, dst_name, mode, uid, gid, src_flags, dst_flags) \
164  copy_file_ext_at(src_name, AT_FDCWD, dst_name, mode, uid, gid, src_flags, dst_flags)
165 #define copy_file libreport_copy_file
166 off_t copy_file(const char *src_name, const char *dst_name, int mode);
167 #define copy_file_at libreport_copy_file_at
168 off_t copy_file_at(const char *src_name, int dir_fd, const char *name, int mode);
169 #define copy_file_recursive libreport_copy_file_recursive
170 int copy_file_recursive(const char *source, const char *dest);
171 
172 #define decompress_fd libreport_decompress_fd
173 int decompress_fd(int fdi, int fdo);
174 #define decompress_file libreport_decompress_file
175 int decompress_file(const char *path_in, const char *path_out, mode_t mode_out);
176 #define decompress_file_ext_at libreport_decompress_file_ext_at
177 int decompress_file_ext_at(const char *path_in, int dir_fd, const char *path_out,
178  mode_t mode_out, uid_t uid, gid_t gid, int src_flags, int dst_flags);
179 
180 // NB: will return short read on error, not -1,
181 // if some data was read before error occurred
182 #define xread libreport_xread
183 void xread(int fd, void *buf, size_t count);
184 #define safe_read libreport_safe_read
185 ssize_t safe_read(int fd, void *buf, size_t count);
186 #define safe_write libreport_safe_write
187 ssize_t safe_write(int fd, const void *buf, size_t count);
188 #define full_read libreport_full_read
189 ssize_t full_read(int fd, void *buf, size_t count);
190 #define full_write libreport_full_write
191 ssize_t full_write(int fd, const void *buf, size_t count);
192 #define full_write_str libreport_full_write_str
193 ssize_t full_write_str(int fd, const char *buf);
194 #define xmalloc_read libreport_xmalloc_read
195 void* xmalloc_read(int fd, size_t *maxsz_p);
196 #define xmalloc_open_read_close libreport_xmalloc_open_read_close
197 void* xmalloc_open_read_close(const char *filename, size_t *maxsz_p);
198 #define xmalloc_xopen_read_close libreport_xmalloc_xopen_read_close
199 void* xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p);
200 #define malloc_readlink libreport_malloc_readlink
201 char* malloc_readlink(const char *linkname);
202 #define malloc_readlinkat libreport_malloc_readlinkat
203 char* malloc_readlinkat(int dir_fd, const char *linkname);
204 
205 
206 /* Returns malloc'ed block */
207 #define encode_base64 libreport_encode_base64
208 char *encode_base64(const void *src, int length);
209 
210 /* Returns NULL if the string needs no sanitizing.
211  * control_chars_to_sanitize is a bit mask.
212  * If Nth bit is set, Nth control char will be sanitized (replaced by [XX]).
213  */
214 #define sanitize_utf8 libreport_sanitize_utf8
215 char *sanitize_utf8(const char *src, uint32_t control_chars_to_sanitize);
216 enum {
217  SANITIZE_ALL = 0xffffffff,
218  SANITIZE_TAB = (1 << 9),
219  SANITIZE_LF = (1 << 10),
220  SANITIZE_CR = (1 << 13),
221 };
222 
223 #define SHA1_RESULT_LEN (5 * 4)
224 typedef struct sha1_ctx_t {
225  uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
226  /* for sha256: void (*process_block)(struct md5_ctx_t*); */
227  uint64_t total64; /* must be directly before hash[] */
228  uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
229 } sha1_ctx_t;
230 #define sha1_begin libreport_sha1_begin
231 void sha1_begin(sha1_ctx_t *ctx);
232 #define sha1_hash libreport_sha1_hash
233 void sha1_hash(sha1_ctx_t *ctx, const void *buffer, size_t len);
234 #define sha1_end libreport_sha1_end
235 void sha1_end(sha1_ctx_t *ctx, void *resbuf);
236 
237 /* Helpers to hash a string: */
238 #define str_to_sha1 libreport_str_to_sha1
239 const uint8_t *str_to_sha1(uint8_t result[SHA1_RESULT_LEN], const char *str);
240 #define str_to_sha1str libreport_str_to_sha1str
241 const char *str_to_sha1str(char result[SHA1_RESULT_LEN*2 + 1], const char *str);
242 
243 
244 #define try_atou libreport_try_atou
245 int try_atou(const char *numstr, unsigned *value);
246 #define xatou libreport_xatou
247 unsigned xatou(const char *numstr);
248 #define try_atoi libreport_try_atoi
249 int try_atoi(const char *numstr, int *value);
250 #define xatoi libreport_xatoi
251 int xatoi(const char *numstr);
252 /* Using xatoi() instead of naive atoi() is not always convenient -
253  * in many places people want *non-negative* values, but store them
254  * in signed int. Therefore we need this one:
255  * dies if input is not in [0, INT_MAX] range. Also will reject '-0' etc.
256  * It should really be named xatoi_nonnegative (since it allows 0),
257  * but that would be too long.
258  */
259 #define try_atoi_positive libreport_try_atoi_positive
260 int try_atoi_positive(const char *numstr, int *value);
261 #define xatoi_positive libreport_xatoi_positive
262 int xatoi_positive(const char *numstr);
263 
264 //unused for now
265 //unsigned long long monotonic_ns(void);
266 //unsigned long long monotonic_us(void);
267 //unsigned monotonic_sec(void);
268 
269 #define safe_waitpid libreport_safe_waitpid
270 pid_t safe_waitpid(pid_t pid, int *wstat, int options);
271 
272 enum {
273  /* on return, pipefds[1] is fd to which parent may write
274  * and deliver data to child's stdin: */
275  EXECFLG_INPUT = 1 << 0,
276  /* on return, pipefds[0] is fd from which parent may read
277  * child's stdout: */
278  EXECFLG_OUTPUT = 1 << 1,
279  /* open child's stdin to /dev/null: */
280  EXECFLG_INPUT_NUL = 1 << 2,
281  /* open child's stdout to /dev/null: */
282  EXECFLG_OUTPUT_NUL = 1 << 3,
283  /* redirect child's stderr to stdout: */
284  EXECFLG_ERR2OUT = 1 << 4,
285  /* open child's stderr to /dev/null: */
286  EXECFLG_ERR_NUL = 1 << 5,
287  /* suppress perror_msg("Can't execute 'foo'") if exec fails */
288  EXECFLG_QUIET = 1 << 6,
289  EXECFLG_SETGUID = 1 << 7,
290  EXECFLG_SETSID = 1 << 8,
291  EXECFLG_SETPGID = 1 << 9,
292 };
293 /*
294  * env_vec: list of variables to set in environment (if string has
295  * "VAR=VAL" form) or unset in environment (if string has no '=' char).
296  *
297  * Returns pid.
298  */
299 #define fork_execv_on_steroids libreport_fork_execv_on_steroids
300 pid_t fork_execv_on_steroids(int flags,
301  char **argv,
302  int *pipefds,
303  char **env_vec,
304  const char *dir,
305  uid_t uid);
306 /* Returns malloc'ed string. NULs are retained, and extra one is appended
307  * after the last byte (this NUL is not accounted for in *size_p) */
308 #define run_in_shell_and_save_output libreport_run_in_shell_and_save_output
309 char *run_in_shell_and_save_output(int flags,
310  const char *cmd,
311  const char *dir,
312  size_t *size_p);
313 
314 /* Random utility functions */
315 
316 #define is_in_string_list libreport_is_in_string_list
317 bool is_in_string_list(const char *name, const char *const *v);
318 
319 #define index_of_string_in_list libreport_index_of_string_in_list
320 int index_of_string_in_list(const char *name, const char *const *v);
321 
322 #define is_in_comma_separated_list libreport_is_in_comma_separated_list
323 bool is_in_comma_separated_list(const char *value, const char *list);
324 #define is_in_comma_separated_list_of_glob_patterns libreport_is_in_comma_separated_list_of_glob_patterns
325 bool is_in_comma_separated_list_of_glob_patterns(const char *value, const char *list);
326 
327 /* Calls GLib version appropriate initialization function.
328  */
329 #define glib_init libreport_glib_init
330 void glib_init(void);
331 
332 /* Frees every element'd data using free(),
333  * then frees list itself using g_list_free(list):
334  */
335 #define list_free_with_free libreport_list_free_with_free
336 void list_free_with_free(GList *list);
337 
338 #define get_dirsize libreport_get_dirsize
339 double get_dirsize(const char *pPath);
340 #define get_dirsize_find_largest_dir libreport_get_dirsize_find_largest_dir
341 double get_dirsize_find_largest_dir(
342  const char *pPath,
343  char **worst_dir, /* can be NULL */
344  const char *excluded /* can be NULL */
345 );
346 
347 #define ndelay_on libreport_ndelay_on
348 int ndelay_on(int fd);
349 #define ndelay_off libreport_ndelay_off
350 int ndelay_off(int fd);
351 #define close_on_exec_on libreport_close_on_exec_on
352 int close_on_exec_on(int fd);
353 
354 #define xmalloc libreport_xmalloc
355 void* xmalloc(size_t size);
356 #define xrealloc libreport_xrealloc
357 void* xrealloc(void *ptr, size_t size);
358 #define xzalloc libreport_xzalloc
359 void* xzalloc(size_t size);
360 #define xstrdup libreport_xstrdup
361 char* xstrdup(const char *s);
362 #define xstrndup libreport_xstrndup
363 char* xstrndup(const char *s, int n);
364 #define xstrdup_between libreport_xstrdup_between
365 char* xstrdup_between(const char *s, const char *open, const char *close);
366 
367 #define xpipe libreport_xpipe
368 void xpipe(int filedes[2]);
369 #define xdup libreport_xdup
370 int xdup(int from);
371 #define xdup2 libreport_xdup2
372 void xdup2(int from, int to);
373 #define xmove_fd libreport_xmove_fd
374 void xmove_fd(int from, int to);
375 
376 #define xwrite libreport_xwrite
377 void xwrite(int fd, const void *buf, size_t count);
378 #define xwrite_str libreport_xwrite_str
379 void xwrite_str(int fd, const char *str);
380 
381 #define xlseek libreport_xlseek
382 off_t xlseek(int fd, off_t offset, int whence);
383 
384 #define xchdir libreport_xchdir
385 void xchdir(const char *path);
386 
387 #define xvasprintf libreport_xvasprintf
388 char* xvasprintf(const char *format, va_list p);
389 #define xasprintf libreport_xasprintf
390 char* xasprintf(const char *format, ...);
391 
392 #define xsetenv libreport_xsetenv
393 void xsetenv(const char *key, const char *value);
394 /*
395  * Utility function to unsetenv a string which was possibly putenv'ed.
396  * The problem here is that "natural" optimization:
397  * strchrnul(var_val, '=')[0] = '\0';
398  * unsetenv(var_val);
399  * is BUGGY: if string was put into environment via putenv,
400  * its modification (s/=/NUL/) is illegal, and unsetenv will fail to unset it.
401  * Of course, saving/restoring the char wouldn't work either.
402  * This helper creates a copy up to '=', unsetenv's it, and frees:
403  */
404 #define safe_unsetenv libreport_safe_unsetenv
405 void safe_unsetenv(const char *var_val);
406 
407 #define xsocket libreport_xsocket
408 int xsocket(int domain, int type, int protocol);
409 #define xbind libreport_xbind
410 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
411 #define xlisten libreport_xlisten
412 void xlisten(int s, int backlog);
413 #define xsendto libreport_xsendto
414 ssize_t xsendto(int s, const void *buf, size_t len,
415  const struct sockaddr *to, socklen_t tolen);
416 
417 #define xstat libreport_xstat
418 void xstat(const char *name, struct stat *stat_buf);
419 #define fstat_st_size_or_die libreport_fstat_st_size_or_die
420 off_t fstat_st_size_or_die(int fd);
421 #define stat_st_size_or_die libreport_stat_st_size_or_die
422 off_t stat_st_size_or_die(const char *filename);
423 
424 #define xopen3 libreport_xopen3
425 int xopen3(const char *pathname, int flags, int mode);
426 #define xopen libreport_xopen
427 int xopen(const char *pathname, int flags);
428 #define xunlink libreport_xunlink
429 void xunlink(const char *pathname);
430 #define xunlinkat libreport_xunlinkat
431 void xunlinkat(int dir_fd, const char *pathname, int flags);
432 
433 /* Just testing dent->d_type == DT_REG is wrong: some filesystems
434  * do not report the type, they report DT_UNKNOWN for every dirent
435  * (and this is not a bug in filesystem, this is allowed by standards).
436  * This function handles this case. Note: it returns 0 on symlinks
437  * even if they point to regular files.
438  */
439 #define is_regular_file libreport_is_regular_file
440 int is_regular_file(struct dirent *dent, const char *dirname);
441 #define is_regular_file_at libreport_is_regular_file_at
442 int is_regular_file_at(struct dirent *dent, int dir_fd);
443 
444 #define dot_or_dotdot libreport_dot_or_dotdot
445 bool dot_or_dotdot(const char *filename);
446 #define last_char_is libreport_last_char_is
447 char *last_char_is(const char *s, int c);
448 
449 #define string_to_bool libreport_string_to_bool
450 bool string_to_bool(const char *s);
451 
452 #define xseteuid libreport_xseteuid
453 void xseteuid(uid_t euid);
454 #define xsetegid libreport_xsetegid
455 void xsetegid(gid_t egid);
456 #define xsetreuid libreport_xsetreuid
457 void xsetreuid(uid_t ruid, uid_t euid);
458 #define xsetregid libreport_xsetregid
459 void xsetregid(gid_t rgid, gid_t egid);
460 
461 
462 /* Emit a string of hex representation of bytes */
463 #define bin2hex libreport_bin2hex
464 char* bin2hex(char *dst, const char *str, int count);
465 /* Convert "xxxxxxxx" hex string to binary, no more than COUNT bytes */
466 #define hex2bin libreport_hex2bin
467 char* hex2bin(char *dst, const char *str, int count);
468 
469 
470 enum {
471  LOGMODE_NONE = 0,
472  LOGMODE_STDIO = (1 << 0),
473  LOGMODE_SYSLOG = (1 << 1),
474  LOGMODE_BOTH = LOGMODE_SYSLOG + LOGMODE_STDIO,
475  LOGMODE_CUSTOM = (1 << 2),
476  LOGMODE_JOURNAL = (1 << 3),
477 };
478 
479 #define g_custom_logger libreport_g_custom_logger
480 extern void (*g_custom_logger)(const char*);
481 #define msg_prefix libreport_msg_prefix
482 extern const char *msg_prefix;
483 #define msg_eol libreport_msg_eol
484 extern const char *msg_eol;
485 #define logmode libreport_logmode
486 extern int logmode;
487 #define xfunc_error_retval libreport_xfunc_error_retval
488 extern int xfunc_error_retval;
489 
490 /* A few magic exit codes */
491 #define EXIT_CANCEL_BY_USER 69
492 #define EXIT_STOP_EVENT_RUN 70
493 
494 #define set_xfunc_error_retval libreport_set_xfunc_error_retval
495 void set_xfunc_error_retval(int retval);
496 
497 /* Verbosity level */
498 #define g_verbose libreport_g_verbose
499 extern int g_verbose;
500 /* VERB1 log("what you sometimes want to see, even on a production box") */
501 #define VERB1 if (g_verbose >= 1)
502 /* VERB2 log("debug message, not going into insanely small details") */
503 #define VERB2 if (g_verbose >= 2)
504 /* VERB3 log("lots and lots of details") */
505 #define VERB3 if (g_verbose >= 3)
506 /* there is no level > 3 */
507 
508 #define libreport_
509 #define xfunc_die libreport_xfunc_die
510 void xfunc_die(void) NORETURN;
511 
512 #define die_out_of_memory libreport_die_out_of_memory
513 void die_out_of_memory(void) NORETURN;
514 
515 /* It's a macro, not function, since it collides with log() from math.h */
516 #undef log
517 #define log(...) log_standard(LOG_WARNING, __FILE__, __LINE__, __func__, __VA_ARGS__)
518 #define log_debug(...) log_standard(LOG_DEBUG, __FILE__, __LINE__, __func__, __VA_ARGS__)
519 #define log_info(...) log_standard(LOG_INFO, __FILE__, __LINE__, __func__, __VA_ARGS__)
520 #define log_notice(...) log_standard(LOG_NOTICE, __FILE__, __LINE__, __func__, __VA_ARGS__)
521 #define log_warning(...) log_standard(LOG_WARNING, __FILE__, __LINE__, __func__, __VA_ARGS__)
522 #define log_error(...) log_standard(LOG_ERR, __FILE__, __LINE__, __func__, __VA_ARGS__)
523 
524 #define log_standard(level, file, line, func, ...) log_wrapper(level, __FILE__, __LINE__, __func__, false, false, __VA_ARGS__)
525 
526 // level, file, line, func, perror, custom logger, format & args
527 #define log_error_and_die(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, false, false,__VA_ARGS__)
528 #define log_perror(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, false, __VA_ARGS__)
529 #define log_perror_and_die(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, false, __VA_ARGS__)
530 
531 #define error_msg(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
532 #define perror_msg(...) log_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
533 #define warn_msg(...) log_wrapper(LOG_WARNING, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
534 #define pwarn_msg(...) log_wrapper(LOG_WARNING, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
535 #define error_msg_and_die(...) log_and_die_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, false, true, __VA_ARGS__)
536 #define perror_msg_and_die(...) log_and_die_wrapper(LOG_ERR, __FILE__, __LINE__, __func__, true, true, __VA_ARGS__)
537 
538 
539 void log_wrapper(int level,
540  const char *file,
541  int line,
542  const char *func,
543  bool process_perror,
544  bool use_custom_logger,
545  const char *format, ...) __attribute__ ((format (printf, 7,8)));
546 
547 void log_and_die_wrapper(int level,
548  const char *file,
549  int line,
550  const char *func,
551  bool process_perror,
552  bool use_custom_logger,
553  const char *format, ...) __attribute__ ((noreturn, format (printf, 7,8)));
554 
555 struct strbuf
556 {
557  /* Size of the allocated buffer. Always > 0. */
558  int alloc;
559  /* Length of the string, without the ending \0. */
560  int len;
561  char *buf;
562 };
563 
570 #define strbuf_new libreport_strbuf_new
571 struct strbuf *strbuf_new(void);
572 
578 #define strbuf_free libreport_strbuf_free
579 void strbuf_free(struct strbuf *strbuf);
580 
586 #define strbuf_free_nobuf libreport_strbuf_free_nobuf
587 char* strbuf_free_nobuf(struct strbuf *strbuf);
588 
593 #define strbuf_clear libreport_strbuf_clear
594 void strbuf_clear(struct strbuf *strbuf);
595 
600 #define strbuf_append_char libreport_strbuf_append_char
601 struct strbuf *strbuf_append_char(struct strbuf *strbuf, char c);
602 
607 #define strbuf_append_str libreport_strbuf_append_str
608 struct strbuf *strbuf_append_str(struct strbuf *strbuf,
609  const char *str);
610 
615 #define strbuf_prepend_str libreport_strbuf_prepend_str
616 struct strbuf *strbuf_prepend_str(struct strbuf *strbuf,
617  const char *str);
618 
623 #define strbuf_append_strf libreport_strbuf_append_strf
624 struct strbuf *strbuf_append_strf(struct strbuf *strbuf,
625  const char *format, ...);
626 
631 #define strbuf_append_strfv libreport_strbuf_append_strfv
632 struct strbuf *strbuf_append_strfv(struct strbuf *strbuf,
633  const char *format, va_list p);
634 
640 #define strbuf_prepend_strf libreport_strbuf_prepend_strf
641 struct strbuf *strbuf_prepend_strf(struct strbuf *strbuf,
642  const char *format, ...);
643 
648 #define strbuf_prepend_strfv libreport_strbuf_prepend_strfv
649 struct strbuf *strbuf_prepend_strfv(struct strbuf *strbuf,
650  const char *format, va_list p);
651 
652 /* Returns command line of running program.
653  * Caller is responsible to free() the returned value.
654  * If the pid is not valid or command line can not be obtained,
655  * empty string is returned.
656  */
657 #define get_cmdline libreport_get_cmdline
658 char* get_cmdline(pid_t pid);
659 #define get_environ libreport_get_environ
660 char* get_environ(pid_t pid);
661 #define get_executable libreport_get_executable
662 char *get_executable(pid_t pid);
663 #define get_cwd libreport_get_cwd
664 char* get_cwd(pid_t pid);
665 #define get_rootdir libreport_get_rootdir
666 char* get_rootdir(pid_t pid);
667 #define get_fsuid libreport_get_fsuid
668 int get_fsuid(const char *proc_pid_status);
669 #define get_fsgid libreport_get_fsgid
670 int get_fsgid(const char *proc_pid_status);
671 #define dump_fd_info_ext libreport_dump_fd_info_ext
672 int dump_fd_info_ext(const char *dest_filename, const char *proc_pid_fd_path, uid_t uid, gid_t gid);
673 #define dump_fd_info libreport_dump_fd_info
674 int dump_fd_info(const char *dest_filename, const char *proc_pid_fd_path);
675 
676 /* Takes ptr to time_t, or NULL if you want to use current time.
677  * Returns "YYYY-MM-DD-hh:mm:ss" string.
678  */
679 #define iso_date_string libreport_iso_date_string
680 char *iso_date_string(const time_t *pt);
681 #define LIBREPORT_ISO_DATE_STRING_SAMPLE "YYYY-MM-DD-hh:mm:ss"
682 
683 enum {
684  MAKEDESC_SHOW_FILES = (1 << 0),
685  MAKEDESC_SHOW_MULTILINE = (1 << 1),
686  MAKEDESC_SHOW_ONLY_LIST = (1 << 2),
687  MAKEDESC_WHITELIST = (1 << 3),
688  /* Include all URLs from FILENAME_REPORTED_TO element in the description text */
689  MAKEDESC_SHOW_URLS = (1 << 4),
690 };
691 #define make_description libreport_make_description
692 char *make_description(problem_data_t *problem_data, char **names_to_skip, unsigned max_text_size, unsigned desc_flags);
693 #define make_description_bz libreport_make_description_bz
694 char* make_description_bz(problem_data_t *problem_data, unsigned max_text_size);
695 #define make_description_logger libreport_make_description_logger
696 char* make_description_logger(problem_data_t *problem_data, unsigned max_text_size);
697 #define make_description_mailx libreport_make_description_mailx
698 char* make_description_mailx(problem_data_t *problem_data, unsigned max_text_size);
699 
700 /* See man os-release(5) for details */
701 #define OSINFO_ID "ID"
702 #define OSINFO_NAME "NAME"
703 #define OSINFO_VERSION_ID "VERSION_ID"
704 #define OSINFO_PRETTY_NAME "PRETTY_NAME"
705 
706 /* @brief Loads a text in format of os-release(5) in to a map
707  *
708  * Function doesn't check for format errors much. It just tries to avoid
709  * program errors. In case of error the function prints out a log message and
710  * continues in parsing.
711  *
712  * @param osinfo_bytes Non-NULL pointer to osinfo bytes.
713  * @param osinfo The map where result is stored
714  */
715 #define parse_osinfo libreport_parse_osinfo
716 void parse_osinfo(const char *osinfo_bytes, map_string_t *osinfo);
717 
718 /* @brief Builds product string and product's version string for Bugzilla
719  *
720  * At first tries to get strings from the os specific variables
721  * (REDHAT_BUGZILLA_PRODUCT, REDHAT_BUGZILLA_PRODUCT_VERSION) if no such
722  * variables are found, uses NAME key for the product and VERSION_ID key for
723  * the product's version. If neither NAME nor VERSION_ID are provided fallbacks
724  * to parsing of os_release which should be stored under PRETTY_NAME key.
725  *
726  * https://bugzilla.redhat.com/show_bug.cgi?id=950373
727  *
728  * @param osinfo Input data from which the values are built
729  * @param produc Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
730  * @param version Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
731  */
732 #define parse_osinfo_for_bz libreport_parse_osinfo_for_bz
733 void parse_osinfo_for_bz(map_string_t *osinfo, char **product, char **version);
734 
735 /* @brief Builds product string and product's version string for Red Hat Support
736  *
737  * At first tries to get strings from the os specific variables
738  * (REDHAT_SUPPORT_PRODUCT, REDHAT_SUPPORT_PRODUCT_VERSION) if no such
739  * variables are found, uses NAME key for the product and VERSION_ID key for
740  * the product's version. If no NAME nor VERSION_ID are provided fallbacks to
741  * parsing of os_release which should be stored under PRETTY_NAME key.
742  *
743  * https://bugzilla.redhat.com/show_bug.cgi?id=950373
744  *
745  * @param osinfo Input data from which the values are built
746  * @param produc Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
747  * @param version Non-NULL pointer where pointer to malloced string will be stored. Memory must be released by free()
748  */
749 #define parse_osinfo_for_rhts libreport_parse_osinfo_for_rhts
750 void parse_osinfo_for_rhts(map_string_t *osinfo, char **product, char **version);
751 
752 #define parse_release_for_bz libreport_parse_release_for_bz
753 void parse_release_for_bz(const char *pRelease, char **product, char **version);
754 #define parse_release_for_rhts libreport_parse_release_for_rhts
755 void parse_release_for_rhts(const char *pRelease, char **product, char **version);
756 
771 #define load_conf_file libreport_load_conf_file
772 bool load_conf_file(const char *pPath, map_string_t *settings, bool skipKeysWithoutValue);
773 
774 #define load_conf_file_from_dirs libreport_load_conf_file_from_dirs
775 bool load_conf_file_from_dirs(const char *base_name, const char *const *directories, map_string_t *settings, bool skipKeysWithoutValue);
776 
777 #define save_conf_file libreport_save_conf_file
778 bool save_conf_file(const char *path, map_string_t *settings);
779 
780 #define save_app_conf_file libreport_save_app_conf_file
781 bool save_app_conf_file(const char* application_name, map_string_t *settings);
782 #define load_app_conf_file libreport_load_app_conf_file
783 bool load_app_conf_file(const char *application_name, map_string_t *settings);
784 #define set_app_user_setting libreport_set_app_user_setting
785 void set_app_user_setting(map_string_t *settings, const char *name, const char *value);
786 #define get_app_user_setting libreport_get_app_user_setting
787 const char *get_app_user_setting(map_string_t *settings, const char *name);
788 
789 #define save_user_settings libreport_save_user_settings
790 bool save_user_settings();
791 #define load_user_settings libreport_load_user_settings
792 bool load_user_settings(const char *application_name);
793 #define set_user_setting libreport_set_user_setting
794 void set_user_setting(const char *name, const char *value);
795 #define get_user_setting libreport_get_user_setting
796 const char *get_user_setting(const char *name);
797 
798 /* filename is expected to exist in CONF_DIR
799  * usually /etc/libreport
800  */
801 #define load_forbidden_words libreport_load_forbidden_words
802 GList *load_words_from_file(const char *filename);
803 #define get_file_list libreport_get_file_list
804 GList *get_file_list(const char *path, const char *ext);
805 #define free_file_list libreport_free_file_list
806 void free_file_list(GList *filelist);
807 #define new_file_obj libreport_new_file_obj
808 file_obj_t *new_file_obj(const char* fullpath, const char* filename);
809 #define free_file_obj libreport_free_file_obj
810 void free_file_obj(file_obj_t *f);
811 #define load_workflow_config_data libreport_load_workflow_config_data
812 GHashTable *load_workflow_config_data(const char* path);
813 #define parse_list libreport_parse_list
814 GList *parse_list(const char* list);
815 
816 /* Connect to abrtd over unix domain socket, issue DELETE command */
817 int delete_dump_dir_possibly_using_abrtd(const char *dump_dir_name);
818 
819 /* Tries to create a copy of dump_dir_name in base_dir, with same or similar basename.
820  * Returns NULL if copying failed. In this case, logs a message before returning. */
821 #define steal_directory libreport_steal_directory
822 struct dump_dir *steal_directory(const char *base_dir, const char *dump_dir_name);
823 
824 /* Tries to open dump_dir_name with writing access. If function needs to steal
825  * directory calls ask_continue(new base dir, dump dir) callback to ask user
826  * for permission. If ask_continue param is NULL the function thinks that an
827  * answer is positive and steals directory.
828  * Returns NULL if opening failed or if stealing was dismissed. In this case,
829  * logs a message before returning. */
830 #define open_directory_for_writing libreport_open_directory_for_writing
831 struct dump_dir *open_directory_for_writing(
832  const char *dump_dir_name,
833  bool (*ask_continue)(const char *, const char *));
834 
835 // Files bigger than this are never considered to be text.
836 //
837 // Started at 64k limit. But _some_ limit is necessary:
838 // fields declared "text" may end up in editing fields and such.
839 // We don't want to accidentally end up with 100meg text in a textbox!
840 // So, don't remove this. If you really need to, raise the limit.
841 //
842 // Bumped up to 200k: saw 124740 byte /proc/PID/smaps file
843 // Bumped up to 500k: saw 375252 byte anaconda traceback file
844 // Bumped up to 1M: bugzilla.redhat.com/show_bug.cgi?id=746727
845 // mentions 853646 byte anaconda-tb-* file.
846 // Bumped up to 8M: bugzilla.redhat.com/show_bug.cgi?id=887570
847 // (anaconda-tb file of 1.38 MBytes)
848 //
849 #define CD_MAX_TEXT_SIZE (8*1024*1024)
850 
851 // Text bigger than this usually is attached, not added inline
852 // was 2k, 20kb is too much, let's try 4kb
853 //
854 // For bug databases
855 #define CD_TEXT_ATT_SIZE_BZ (4*1024)
856 // For dumping problem data into a text file, email, etc
857 #define CD_TEXT_ATT_SIZE_LOGGER (CD_MAX_TEXT_SIZE)
858 
859 // Filenames in problem directory:
860 // filled by a hook:
861 #define FILENAME_TIME "time" /* mandatory */
862 #define FILENAME_LAST_OCCURRENCE "last_occurrence" /* optional */
863 #define FILENAME_REASON "reason" /* mandatory? */
864 #define FILENAME_UID "uid" /* mandatory? */
865 /*
866  * "analyzer" is to be gradually changed to "type":
867  * For now, we fetch and look at "analyzer" element,
868  * but we always save both "analyzer" and "type" (with same contents).
869  * By 2013, we switch to looking at "type". Then we will stop generating
870  * "analyzer" element.
871  */
872 #define FILENAME_ANALYZER "analyzer"
873 #define FILENAME_TYPE "type"
874 #define FILENAME_EXECUTABLE "executable"
875 #define FILENAME_PID "pid"
876 #define FILENAME_PWD "pwd"
877 #define FILENAME_ROOTDIR "rootdir"
878 #define FILENAME_BINARY "binary"
879 #define FILENAME_CMDLINE "cmdline"
880 #define FILENAME_COREDUMP "coredump"
881 #define FILENAME_CGROUP "cgroup"
882 #define FILENAME_BACKTRACE "backtrace"
883 #define FILENAME_MAPS "maps"
884 #define FILENAME_SMAPS "smaps"
885 #define FILENAME_PROC_PID_STATUS "proc_pid_status"
886 #define FILENAME_ENVIRON "environ"
887 #define FILENAME_LIMITS "limits"
888 #define FILENAME_OPEN_FDS "open_fds"
889 
890 /* Global problem identifier which is usually generated by some "analyze_*"
891  * event because it may take a lot of time to obtain strong problem
892  * identification */
893 #define FILENAME_DUPHASH "duphash"
894 
895 // Name of the function where the application crashed.
896 // Optional.
897 #define FILENAME_CRASH_FUNCTION "crash_function"
898 #define FILENAME_ARCHITECTURE "architecture"
899 #define FILENAME_KERNEL "kernel"
900 /*
901  * From /etc/os-release
902  * os_release filename name is alredy occupied by /etc/redhat-release (see
903  * below) in sake of backward compatibility /etc/os-release is stored in
904  * os_info file
905  */
906 #define FILENAME_OS_INFO "os_info"
907 #define FILENAME_OS_INFO_IN_ROOTDIR "os_info_in_rootdir"
908 // From /etc/system-release or /etc/redhat-release
909 #define FILENAME_OS_RELEASE "os_release"
910 #define FILENAME_OS_RELEASE_IN_ROOTDIR "os_release_in_rootdir"
911 // Filled by <what?>
912 #define FILENAME_PACKAGE "package"
913 #define FILENAME_COMPONENT "component"
914 #define FILENAME_COMMENT "comment"
915 #define FILENAME_RATING "backtrace_rating"
916 #define FILENAME_HOSTNAME "hostname"
917 // Optional. Set to "1" by abrt-handle-upload for every unpacked dump
918 #define FILENAME_REMOTE "remote"
919 #define FILENAME_TAINTED "kernel_tainted"
920 #define FILENAME_TAINTED_SHORT "kernel_tainted_short"
921 #define FILENAME_TAINTED_LONG "kernel_tainted_long"
922 #define FILENAME_VMCORE "vmcore"
923 #define FILENAME_KERNEL_LOG "kernel_log"
924 // File created by createAlertSignature() from libreport's python module
925 // The file should contain a description of an alert
926 #define FILENAME_DESCRIPTION "description"
927 
928 /* Local problem identifier (weaker than global identifier) designed for fast
929  * local for fast local duplicate identification. This file is usually provided
930  * by crashed application (problem creator).
931  */
932 #define FILENAME_UUID "uuid"
933 
934 #define FILENAME_COUNT "count"
935 /* Multi-line list of places problem was reported.
936  * Recommended line format:
937  * "Reporter: VAR=VAL VAR=VAL"
938  * Use add_reported_to(dd, "line_without_newline"): it adds line
939  * only if it is not already there.
940  */
941 #define FILENAME_REPORTED_TO "reported_to"
942 #define FILENAME_EVENT_LOG "event_log"
943 /*
944  * If exists, should contain a full sentence (with trailing period)
945  * which describes why this problem should not be reported.
946  * Example: "Your laptop firmware 1.9a is buggy, version 1.10 contains the fix."
947  */
948 #define FILENAME_NOT_REPORTABLE "not-reportable"
949 #define FILENAME_CORE_BACKTRACE "core_backtrace"
950 #define FILENAME_REMOTE_RESULT "remote_result"
951 #define FILENAME_PKG_EPOCH "pkg_epoch"
952 #define FILENAME_PKG_NAME "pkg_name"
953 #define FILENAME_PKG_VERSION "pkg_version"
954 #define FILENAME_PKG_RELEASE "pkg_release"
955 #define FILENAME_PKG_ARCH "pkg_arch"
956 #define FILENAME_USERNAME "username"
957 #define FILENAME_ABRT_VERSION "abrt_version"
958 #define FILENAME_EXPLOITABLE "exploitable"
959 
960 /* File names related to Anaconda problems
961  */
962 #define FILENAME_KICKSTART_CFG "ks.cfg"
963 #define FILENAME_ANACONDA_TB "anaconda-tb"
964 
965 // Not stored as files, added "on the fly":
966 #define CD_DUMPDIR "Directory"
967 
968 #define cmp_problem_data libreport_cmp_problem_data
969 gint cmp_problem_data(gconstpointer a, gconstpointer b, gpointer filename);
970 
971 //UNUSED:
974 //#define CD_EVENTS "Events"
975 
976 /* FILENAME_EVENT_LOG is trimmed to below LOW_WATERMARK
977  * when it reaches HIGH_WATERMARK size
978  */
979 enum {
980  EVENT_LOG_HIGH_WATERMARK = 30 * 1024,
981  EVENT_LOG_LOW_WATERMARK = 20 * 1024,
982 };
983 
984 #define log_problem_data libreport_log_problem_data
985 void log_problem_data(problem_data_t *problem_data, const char *pfx);
986 
987 extern int g_libreport_inited;
988 void libreport_init(void);
989 
990 #define INITIALIZE_LIBREPORT() \
991  do \
992  { \
993  if (!g_libreport_inited) \
994  { \
995  g_libreport_inited = 1; \
996  libreport_init(); \
997  } \
998  } \
999  while (0)
1000 
1001 const char *abrt_init(char **argv);
1002 #define export_abrt_envvars libreport_export_abrt_envvars
1003 void export_abrt_envvars(int pfx);
1004 #define g_progname libreport_g_progname
1005 extern const char *g_progname;
1006 
1007 enum parse_opt_type {
1008  OPTION_BOOL,
1009  OPTION_GROUP,
1010  OPTION_STRING,
1011  OPTION_INTEGER,
1012  OPTION_OPTSTRING,
1013  OPTION_LIST,
1014  OPTION_END,
1015 };
1016 
1017 struct options {
1018  enum parse_opt_type type;
1019  int short_name;
1020  const char *long_name;
1021  void *value;
1022  const char *argh;
1023  const char *help;
1024 };
1025 
1026 /*
1027  * s - short_name
1028  * l - long_name
1029  * v - value
1030  * a - option parameter name (for help text)
1031  * h - help
1032  */
1033 #define OPT_END() { OPTION_END }
1034 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
1035 #define OPT_BOOL( s, l, v, h) { OPTION_BOOL , (s), (l), (v), NULL , (h) }
1036 #define OPT_INTEGER( s, l, v, h) { OPTION_INTEGER , (s), (l), (v), "NUM", (h) }
1037 #define OPT_STRING( s, l, v, a, h) { OPTION_STRING , (s), (l), (v), (a) , (h) }
1038 #define OPT_OPTSTRING(s, l, v, a, h) { OPTION_OPTSTRING, (s), (l), (v), (a) , (h) }
1039 #define OPT_LIST( s, l, v, a, h) { OPTION_LIST , (s), (l), (v), (a) , (h) }
1040 
1041 #define OPT__VERBOSE(v) OPT_BOOL('v', "verbose", (v), _("Be verbose"))
1042 #define OPT__DUMP_DIR(v) OPT_STRING('d', "problem-dir", (v), "DIR", _("Problem directory"))
1043 
1044 #define parse_opts libreport_parse_opts
1045 unsigned parse_opts(int argc, char **argv, const struct options *opt,
1046  const char *usage);
1047 
1048 #define show_usage_and_die libreport_show_usage_and_die
1049 void show_usage_and_die(const char *usage, const struct options *opt) NORETURN;
1050 
1051 /* Can't include "abrt_curl.h", it's not a public API.
1052  * Resorting to just forward-declaring the struct we need.
1053  */
1054 struct abrt_post_state;
1055 
1056 #ifdef __cplusplus
1057 }
1058 #endif
1059 
1060 #endif