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