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