libreport  2.5.0
A tool to inform users about various problems on the running system
dump_dir.h
1 /*
2  On-disk storage of problem data
3 
4  Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com)
5  Copyright (C) 2009 RedHat inc.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 #ifndef LIBREPORT_DUMP_DIR_H_
22 #define LIBREPORT_DUMP_DIR_H_
23 
24 /* For DIR */
25 #include <sys/types.h>
26 #include <dirent.h>
27 
28 /* Fore GList */
29 #include <glib.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /* Utility function */
36 int create_symlink_lockfile(const char *filename, const char *pid_str);
37 
38 enum {
39  DD_FAIL_QUIETLY_ENOENT = (1 << 0),
40  DD_FAIL_QUIETLY_EACCES = (1 << 1),
41  /* Open symlinks. dd_* funcs don't open symlinks by default */
42  DD_OPEN_FOLLOW = (1 << 2),
43  DD_OPEN_READONLY = (1 << 3),
44  DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE = (1 << 4),
45  DD_DONT_WAIT_FOR_LOCK = (1 << 5),
46 };
47 
48 struct dump_dir {
49  char *dd_dirname;
50  DIR *next_dir;
51  int locked;
52  uid_t dd_uid;
53  gid_t dd_gid;
54  /* mode of saved files */
55  mode_t mode;
56  time_t dd_time;
57  char *dd_type;
58 
59  /* In case of recursive locking the first caller owns the lock and is
60  * responsible for unlocking. The consecutive dd_lock() callers acquire the
61  * lock but are not able to unlock the dump directory.
62  */
63  int owns_lock;
64 };
65 
66 void dd_close(struct dump_dir *dd);
67 
68 struct dump_dir *dd_opendir(const char *dir, int flags);
69 /* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
70  * (IOW: if you aren't running under root):
71  */
72 struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode);
73 
74 void dd_create_basic_files(struct dump_dir *dd, uid_t uid, const char *chroot_dir);
75 int dd_exist(const struct dump_dir *dd, const char *path);
76 void dd_sanitize_mode_and_owner(struct dump_dir *dd);
77 
78 DIR *dd_init_next_file(struct dump_dir *dd);
79 int dd_get_next_file(struct dump_dir *dd, char **short_name, char **full_name);
80 
81 char* dd_load_text_ext(const struct dump_dir *dd, const char *name, unsigned flags);
82 char* dd_load_text(const struct dump_dir *dd, const char *name);
83 void dd_save_text(struct dump_dir *dd, const char *name, const char *data);
84 void dd_save_binary(struct dump_dir *dd, const char *name, const char *data, unsigned size);
85 int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path);
86 int dd_copy_file_unpack(struct dump_dir *dd, const char *name, const char *source_path);
87 /* Returns value less than 0 if any error occured; otherwise returns size of an
88  * item in Bytes. If an item does not exist returns 0 instead of an error
89  * value.
90  */
91 long dd_get_item_size(struct dump_dir *dd, const char *name);
92 /* Deletes an item from dump directory
93  * On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
94  * For more about errno see unlink documentation
95  */
96 int dd_delete_item(struct dump_dir *dd, const char *name);
97 /* Returns 0 if directory is deleted or not found */
98 int dd_delete(struct dump_dir *dd);
99 int dd_rename(struct dump_dir *dd, const char *new_path);
100 /* Changes owner of dump dir
101  * Uses two different strategies selected at build time by
102  * DUMP_DIR_OWNED_BY_USER configuration:
103  * <= 0 : owner = abrt user's uid, group = new_uid's gid
104  * > 0 : owner = new_uid, group = abrt group's gid
105  *
106  * On success, zero is returned. On error, -1 is returned.
107  */
108 int dd_chown(struct dump_dir *dd, uid_t new_uid);
109 
110 
111 /* reported_to handling */
112 #define add_reported_to_data libreport_add_reported_to_data
113 int add_reported_to_data(char **reported_to, const char *line);
114 #define add_reported_to libreport_add_reported_to
115 void add_reported_to(struct dump_dir *dd, const char *line);
117  char *label;
118  char *url;
119  char *msg;
120  char *bthash;
121  /* char *whole_line; */
122  /* time_t timestamp; */
123  /* ^^^ if you add more fields, don't forget to update free_report_result() */
124 };
125 typedef struct report_result report_result_t;
126 #define free_report_result libreport_free_report_result
127 void free_report_result(struct report_result *result);
128 #define find_in_reported_to_data libreport_find_in_reported_to_data
129 report_result_t *find_in_reported_to_data(const char *reported_to, const char *report_label);
130 #define find_in_reported_to libreport_find_in_reported_to
131 report_result_t *find_in_reported_to(struct dump_dir *dd, const char *report_label);
132 #define read_entire_reported_to_data libreport_read_entire_reported_to_data
133 GList *read_entire_reported_to_data(const char* reported_to);
134 #define read_entire_reported_to libreport_read_entire_reported_to
135 GList *read_entire_reported_to(struct dump_dir *dd);
136 
137 
138 void delete_dump_dir(const char *dirname);
139 /* Checks dump dir accessibility for particular uid.
140  *
141  * If the directory doesn't exist the directory is not accessible and errno is
142  * set to ENOTDIR.
143  *
144  * Returns non zero if dump dir is accessible otherwise return 0 value.
145  */
146 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
147 
148 enum {
149  DD_STAT_ACCESSIBLE_BY_UID = 1,
150  DD_STAT_OWNED_BY_UID = DD_STAT_ACCESSIBLE_BY_UID << 1,
151 };
152 
153 /* Gets information about a dump directory for particular uid.
154  *
155  * If the directory doesn't exist the directory is not accessible and errno is
156  * set to ENOTDIR.
157  *
158  * Returns negative number if error occurred otherwise returns 0 or positive number.
159  */
160 int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
161 
162 /* creates not_reportable file in the problem directory and saves the
163  reason to it, which prevents libreport from reporting the problem
164  On success, zero is returned.
165  On error, -1 is returned and an error message is logged.
166  - this could probably happen only if the dump dir is not locked
167 */
168 int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
169 
170 typedef int (*save_data_call_back)(struct dump_dir *, void *args);
171 
172 /* Saves data in a new dump directory
173  *
174  * Creates a new dump directory in "problem dump location", adds the basic
175  * information to the new directory, calls given callback to allow callees to
176  * customize the dump dir contents (save problem data) and commits the dump
177  * directory (makes the directory visible for a problem daemon).
178  */
179 struct dump_dir *create_dump_dir(const char *base_dir_name, const char *type,
180  uid_t uid, save_data_call_back save_data, void *args);
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif