libreport  2.1.11.1
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 const_string_vector_const_ptr_t */
25 #include "libreport_types.h"
26 
27 #include <stdint.h>
28 #include <stdio.h>
29 
30 /* For DIR */
31 #include <sys/types.h>
32 #include <dirent.h>
33 
34 /* Fore GList */
35 #include <glib.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /* Utility function */
42 int create_symlink_lockfile(const char *filename, const char *pid_str);
43 int create_symlink_lockfile_at(int dir_fd, const char *filename,
44  const char *pid_str, bool log_all_warnings);
45 
46 /* Opens filename for reading relatively to a directory represented by dir_fd.
47  * The function fails if the file is symbolic link, directory or hard link.
48  */
49 int secure_openat_read(int dir_fd, const char *filename);
50 
51 enum {
52  DD_FAIL_QUIETLY_ENOENT = (1 << 0),
53  DD_FAIL_QUIETLY_EACCES = (1 << 1),
54  /* Open symlinks. dd_* funcs don't open symlinks by default */
55  DD_OPEN_FOLLOW = (1 << 2),
56  DD_OPEN_READONLY = (1 << 3),
57  DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE = (1 << 4),
58  DD_DONT_WAIT_FOR_LOCK = (1 << 5),
59  /* Create the new dump directory with parent directories (mkdir -p)*/
60  DD_CREATE_PARENTS = (1 << 6),
61 };
62 
63 struct dump_dir {
64  char *dd_dirname;
65  DIR *next_dir;
66  int locked;
67  uid_t dd_uid;
68  gid_t dd_gid;
69  /* mode of saved files */
70  mode_t mode;
71  time_t dd_time;
72  char *dd_type;
73  int dd_fd;
74 };
75 
76 void dd_close(struct dump_dir *dd);
77 
78 /* Opens the given path and returns the resulting file descriptor.
79  */
80 int dd_openfd(const char *dir);
81 /* Opens the given path
82  */
83 struct dump_dir *dd_opendir(const char *dir, int flags);
84 
85 /* Re-opens a dump_dir opened with DD_OPEN_FD_ONLY.
86  *
87  * The passed dump_dir must not be used any more and the return value must be
88  * used instead.
89  *
90  * The passed flags must not contain DD_OPEN_FD_ONLY.
91  *
92  * The passed dump_dir must not be already locked.
93  */
94 
95 /* Skips dd_openfd(dir) and uses the given file descriptor instead
96  */
97 struct dump_dir *dd_fdopendir(int dir_fd, const char *dir, int flags);
98 
99 struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode, int flags);
100 int dd_reset_ownership(struct dump_dir *dd);
101 /* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
102  * (IOW: if you aren't running under root):
103  */
104 struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode);
105 
106 void dd_create_basic_files(struct dump_dir *dd, uid_t uid, const char *chroot_dir);
107 int dd_exist(const struct dump_dir *dd, const char *path);
108 void dd_sanitize_mode_and_owner(struct dump_dir *dd);
109 
110 DIR *dd_init_next_file(struct dump_dir *dd);
111 int dd_get_next_file(struct dump_dir *dd, char **short_name, char **full_name);
112 
113 char* dd_load_text_ext(const struct dump_dir *dd, const char *name, unsigned flags);
114 char* dd_load_text(const struct dump_dir *dd, const char *name);
115 void dd_save_text(struct dump_dir *dd, const char *name, const char *data);
116 void dd_save_binary(struct dump_dir *dd, const char *name, const char *data, unsigned size);
117 int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path);
118 /* Returns value less than 0 if any error occured; otherwise returns size of an
119  * item in Bytes. If an item does not exist returns 0 instead of an error
120  * value.
121  */
122 long dd_get_item_size(struct dump_dir *dd, const char *name);
123 /* Deletes an item from dump directory
124  * On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
125  * For more about errno see unlink documentation
126  */
127 int dd_delete_item(struct dump_dir *dd, const char *name);
128 
129 /* Returns a file descriptor for the given name. The function is limited to open
130  * an element read only, write only or create new.
131  *
132  * O_RDONLY - opens an existing item for reading
133  * O_RDWR - removes an item, creates its file and opens the file for reading and writing
134  *
135  * @param dd Dump directory
136  * @param name The name of the item
137  * @param flags One of these : O_RDONLY, O_RDWR
138  * @return Negative number on error
139  */
140 int dd_open_item(struct dump_dir *dd, const char *name, int flags);
141 
142 /* Returns a FILE for the given name. The function is limited to open
143  * an element read only, write only or create new.
144  *
145  * O_RDONLY - opens an existing file for reading
146  * O_RDWR - removes an item, creates its file and opens the file for reading and writing
147  *
148  * @param dd Dump directory
149  * @param name The name of the item
150  * @param flags One of these : O_RDONLY, O_RDWR
151  * @return NULL on error
152  */
153 FILE *dd_open_item_file(struct dump_dir *dd, const char *name, int flags);
154 
155 /* Returns 0 if directory is deleted or not found */
156 int dd_delete(struct dump_dir *dd);
157 int dd_rename(struct dump_dir *dd, const char *new_path);
158 /* Changes owner of dump dir
159  * Uses two different strategies selected at build time by
160  * DUMP_DIR_OWNED_BY_USER configuration:
161  * <= 0 : owner = abrt user's uid, group = new_uid's gid
162  * > 0 : owner = new_uid, group = abrt group's gid
163  *
164  * On success, zero is returned. On error, -1 is returned.
165  */
166 int dd_chown(struct dump_dir *dd, uid_t new_uid);
167 
168 
169 /* reported_to handling */
171  char *label;
172  char *url;
173  char *msg;
174  char *bthash;
175  time_t timestamp;
176  /* ^^^ if you add more fields, don't forget to update free_report_result() */
177 };
178 typedef struct report_result report_result_t;
179 
180 /* Appends a new unique line to the list of report results
181  *
182  * If the reported_to data already contains the given line, the line will not
183  * be added again.
184  *
185  * @param reported_to The data
186  * @param line The appended line
187  * @return 1 if the line was added at the end of the reported_to; otherwise 0.
188  */
189 #define add_reported_to_data libreport_add_reported_to_data
190 int add_reported_to_data(char **reported_to, const char *line);
191 
192 /* Appends a new unique entry to the list of report results
193  *
194  * result->label must be non-empty string which does not contain ':' character.
195  *
196  * The function converts the result to a valid reported_to line and calls
197  * add_reported_to_data().
198  *
199  * @param reported_to The data
200  * @param result The appended entry
201  * @return -EINVAL if result->label is invalid; otherwise return value of
202  * add_reported_to_data
203  */
204 #define add_reported_to_entry_data libreport_add_reported_to_entry_data
205 int add_reported_to_entry_data(char **reported_to, struct report_result *result);
206 
207 /* This is a wrapper of add_reported_to_data which accepts 'struct dump_dir *'
208  * in the first argument instead of 'char **'. The added line is stored in
209  * 'reported_to' dump directory file.
210  */
211 #define add_reported_to libreport_add_reported_to
212 void add_reported_to(struct dump_dir *dd, const char *line);
213 
214 /* This is a wrapper of add_reported_to_entry_data which accepts 'struct
215  * dump_dir *' in the first argument instead of 'char **'. The added entry is
216  * stored in 'reported_to' dump directory file.
217  */
218 #define add_reported_to_entry libreport_add_reported_to_entry
219 void add_reported_to_entry(struct dump_dir *dd, struct report_result *result);
220 
221 #define free_report_result libreport_free_report_result
222 void free_report_result(struct report_result *result);
223 #define find_in_reported_to_data libreport_find_in_reported_to_data
224 report_result_t *find_in_reported_to_data(const char *reported_to, const char *report_label);
225 #define find_in_reported_to libreport_find_in_reported_to
226 report_result_t *find_in_reported_to(struct dump_dir *dd, const char *report_label);
227 #define read_entire_reported_to_data libreport_read_entire_reported_to_data
228 GList *read_entire_reported_to_data(const char* reported_to);
229 #define read_entire_reported_to libreport_read_entire_reported_to
230 GList *read_entire_reported_to(struct dump_dir *dd);
231 
232 
233 void delete_dump_dir(const char *dirname);
234 /* Checks dump dir accessibility for particular uid.
235  *
236  * If the directory doesn't exist the directory is not accessible and errno is
237  * set to ENOTDIR.
238  *
239  * Returns non zero if dump dir is accessible otherwise return 0 value.
240  */
241 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
242 int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid);
243 
244 enum {
245  DD_STAT_ACCESSIBLE_BY_UID = 1,
246  DD_STAT_OWNED_BY_UID = DD_STAT_ACCESSIBLE_BY_UID << 1,
247 };
248 
249 /* Gets information about a dump directory for particular uid.
250  *
251  * If the directory doesn't exist the directory is not accessible and errno is
252  * set to ENOTDIR.
253  *
254  * Returns negative number if error occurred otherwise returns 0 or positive number.
255  */
256 int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
257 int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
258 
259 /* creates not_reportable file in the problem directory and saves the
260  reason to it, which prevents libreport from reporting the problem
261  On success, zero is returned.
262  On error, -1 is returned and an error message is logged.
263  - this could probably happen only if the dump dir is not locked
264 */
265 int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
266 
267 /* Creates a new archive from the dump directory contents
268  *
269  * The dd argument must be opened for reading.
270  *
271  * The archive_name must not exist. The file will be created with 0600 mode.
272  *
273  * The archive type is deduced from archive_name suffix. The supported archive
274  * suffixes are the following:
275  * - '.tag.gz' (note: the implementation uses child gzip process)
276  *
277  * The archive will include only the files that are not in the exclude_elements
278  * list. See get_global_always_excluded_elements().
279  *
280  * The argument "flags" is currently unused.
281  *
282  * @return 0 on success; otherwise non-0 value. -ENOSYS if archive type is not
283  * supported. -EEXIST if the archive file already exists. -ECHILD if child
284  * process fails. Other negative values can be converted to errno values by
285  * turning them positive.
286  */
287 int dd_create_archive(struct dump_dir *dd, const char *archive_name,
288  const_string_vector_const_ptr_t exclude_elements, int flags);
289 
290 #ifdef __cplusplus
291 }
292 #endif
293 
294 #endif