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 int dd_copy_file(struct dump_dir *dd, const char *name, const char *source_path);
100 /* Returns value less than 0 if any error occured; otherwise returns size of an
101  * item in Bytes. If an item does not exist returns 0 instead of an error
102  * value.
103  */
104 long dd_get_item_size(struct dump_dir *dd, const char *name);
105 /* Deletes an item from dump directory
106  * On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
107  * For more about errno see unlink documentation
108  */
109 int dd_delete_item(struct dump_dir *dd, const char *name);
110 /* Returns 0 if directory is deleted or not found */
111 int dd_delete(struct dump_dir *dd);
112 int dd_rename(struct dump_dir *dd, const char *new_path);
113 /* Changes owner of dump dir
114  * Uses two different strategies selected at build time by
115  * DUMP_DIR_OWNED_BY_USER configuration:
116  * <= 0 : owner = abrt user's uid, group = new_uid's gid
117  * > 0 : owner = new_uid, group = abrt group's gid
118  *
119  * On success, zero is returned. On error, -1 is returned.
120  */
121 int dd_chown(struct dump_dir *dd, uid_t new_uid);
122 
123 
124 /* reported_to handling */
126  char *label;
127  char *url;
128  char *msg;
129  char *bthash;
130  time_t timestamp;
131  /* ^^^ if you add more fields, don't forget to update free_report_result() */
132 };
133 typedef struct report_result report_result_t;
134 
135 /* Appends a new unique line to the list of report results
136  *
137  * If the reported_to data already contains the given line, the line will not
138  * be added again.
139  *
140  * @param reported_to The data
141  * @param line The appended line
142  * @return 1 if the line was added at the end of the reported_to; otherwise 0.
143  */
144 #define add_reported_to_data libreport_add_reported_to_data
145 int add_reported_to_data(char **reported_to, const char *line);
146 
147 /* Appends a new unique entry to the list of report results
148  *
149  * result->label must be non-empty string which does not contain ':' character.
150  *
151  * The function converts the result to a valid reported_to line and calls
152  * add_reported_to_data().
153  *
154  * @param reported_to The data
155  * @param result The appended entry
156  * @return -EINVAL if result->label is invalid; otherwise return value of
157  * add_reported_to_data
158  */
159 #define add_reported_to_entry_data libreport_add_reported_to_entry_data
160 int add_reported_to_entry_data(char **reported_to, struct report_result *result);
161 
162 /* This is a wrapper of add_reported_to_data which accepts 'struct dump_dir *'
163  * in the first argument instead of 'char **'. The added line is stored in
164  * 'reported_to' dump directory file.
165  */
166 #define add_reported_to libreport_add_reported_to
167 void add_reported_to(struct dump_dir *dd, const char *line);
168 
169 /* This is a wrapper of add_reported_to_entry_data which accepts 'struct
170  * dump_dir *' in the first argument instead of 'char **'. The added entry is
171  * stored in 'reported_to' dump directory file.
172  */
173 #define add_reported_to_entry libreport_add_reported_to_entry
174 void add_reported_to_entry(struct dump_dir *dd, struct report_result *result);
175 
176 #define free_report_result libreport_free_report_result
177 void free_report_result(struct report_result *result);
178 #define find_in_reported_to_data libreport_find_in_reported_to_data
179 report_result_t *find_in_reported_to_data(const char *reported_to, const char *report_label);
180 #define find_in_reported_to libreport_find_in_reported_to
181 report_result_t *find_in_reported_to(struct dump_dir *dd, const char *report_label);
182 #define read_entire_reported_to_data libreport_read_entire_reported_to_data
183 GList *read_entire_reported_to_data(const char* reported_to);
184 #define read_entire_reported_to libreport_read_entire_reported_to
185 GList *read_entire_reported_to(struct dump_dir *dd);
186 
187 
188 void delete_dump_dir(const char *dirname);
189 /* Checks dump dir accessibility for particular uid.
190  *
191  * If the directory doesn't exist the directory is not accessible and errno is
192  * set to ENOTDIR.
193  *
194  * Returns non zero if dump dir is accessible otherwise return 0 value.
195  */
196 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
197 int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid);
198 
199 enum {
200  DD_STAT_ACCESSIBLE_BY_UID = 1,
201  DD_STAT_OWNED_BY_UID = DD_STAT_ACCESSIBLE_BY_UID << 1,
202 };
203 
204 /* Gets information about a dump directory for particular uid.
205  *
206  * If the directory doesn't exist the directory is not accessible and errno is
207  * set to ENOTDIR.
208  *
209  * Returns negative number if error occurred otherwise returns 0 or positive number.
210  */
211 int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
212 int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
213 
214 /* creates not_reportable file in the problem directory and saves the
215  reason to it, which prevents libreport from reporting the problem
216  On success, zero is returned.
217  On error, -1 is returned and an error message is logged.
218  - this could probably happen only if the dump dir is not locked
219 */
220 int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
221 
222 /* Creates a new archive from the dump directory contents
223  *
224  * The dd argument must be opened for reading.
225  *
226  * The archive_name must not exist. The file will be created with 0600 mode.
227  *
228  * The archive type is deduced from archive_name suffix. The supported archive
229  * suffixes are the following:
230  * - '.tag.gz' (note: the implementation uses child gzip process)
231  *
232  * The archive will include only the files that are not in the exclude_elements
233  * list. See get_global_always_excluded_elements().
234  *
235  * The argument "flags" is currently unused.
236  *
237  * @return 0 on success; otherwise non-0 value. -ENOSYS if archive type is not
238  * supported. -EEXIST if the archive file already exists. -ECHILD if child
239  * process fails. Other negative values can be converted to errno values by
240  * turning them positive.
241  */
242 int dd_create_archive(struct dump_dir *dd, const char *archive_name,
243  const_string_vector_const_ptr_t exclude_elements, int flags);
244 
245 #ifdef __cplusplus
246 }
247 #endif
248 
249 #endif