libreport  2.1.11
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 int create_symlink_lockfile_at(int dir_fd, const char *filename, const char *pid_str);
38 
39 /* Opens filename for reading relatively to a directory represented by dir_fd.
40  * The function fails if the file is symbolic link, directory or hard link.
41  */
42 int secure_openat_read(int dir_fd, const char *filename);
43 
44 enum {
45  DD_FAIL_QUIETLY_ENOENT = (1 << 0),
46  DD_FAIL_QUIETLY_EACCES = (1 << 1),
47  /* Open symlinks. dd_* funcs don't open symlinks by default */
48  DD_OPEN_FOLLOW = (1 << 2),
49  DD_OPEN_READONLY = (1 << 3),
50  DD_LOAD_TEXT_RETURN_NULL_ON_FAILURE = (1 << 4),
51  DD_DONT_WAIT_FOR_LOCK = (1 << 5),
52  /* Create the new dump directory with parent directories (mkdir -p)*/
53  DD_CREATE_PARENTS = (1 << 6),
54 };
55 
56 struct dump_dir {
57  char *dd_dirname;
58  DIR *next_dir;
59  int locked;
60  uid_t dd_uid;
61  gid_t dd_gid;
62  /* mode of saved files */
63  mode_t mode;
64  time_t dd_time;
65  char *dd_type;
66  int dd_fd;
67 };
68 
69 void dd_close(struct dump_dir *dd);
70 
71 /* Opens the given path and returns the resulting file descriptor.
72  */
73 int dd_openfd(const char *dir);
74 struct dump_dir *dd_opendir(const char *dir, int flags);
75 /* Skips dd_openfd(dir) and uses the given file descriptor instead
76  */
77 struct dump_dir *dd_fdopendir(int dir_fd, const char *dir, int flags);
78 struct dump_dir *dd_create_skeleton(const char *dir, uid_t uid, mode_t mode, int flags);
79 int dd_reset_ownership(struct dump_dir *dd);
80 /* Pass uid = (uid_t)-1L to disable chown'ing of newly created files
81  * (IOW: if you aren't running under root):
82  */
83 struct dump_dir *dd_create(const char *dir, uid_t uid, mode_t mode);
84 
85 void dd_create_basic_files(struct dump_dir *dd, uid_t uid, const char *chroot_dir);
86 int dd_exist(const struct dump_dir *dd, const char *path);
87 void dd_sanitize_mode_and_owner(struct dump_dir *dd);
88 
89 DIR *dd_init_next_file(struct dump_dir *dd);
90 int dd_get_next_file(struct dump_dir *dd, char **short_name, char **full_name);
91 
92 char* dd_load_text_ext(const struct dump_dir *dd, const char *name, unsigned flags);
93 char* dd_load_text(const struct dump_dir *dd, const char *name);
94 void dd_save_text(struct dump_dir *dd, const char *name, const char *data);
95 void dd_save_binary(struct dump_dir *dd, const char *name, const char *data, unsigned size);
96 /* Returns value less than 0 if any error occured; otherwise returns size of an
97  * item in Bytes. If an item does not exist returns 0 instead of an error
98  * value.
99  */
100 long dd_get_item_size(struct dump_dir *dd, const char *name);
101 /* Deletes an item from dump directory
102  * On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
103  * For more about errno see unlink documentation
104  */
105 int dd_delete_item(struct dump_dir *dd, const char *name);
106 /* Returns 0 if directory is deleted or not found */
107 int dd_delete(struct dump_dir *dd);
108 int dd_rename(struct dump_dir *dd, const char *new_path);
109 /* Changes owner of dump dir
110  * Uses two different strategies selected at build time by
111  * DUMP_DIR_OWNED_BY_USER configuration:
112  * <= 0 : owner = abrt user's uid, group = new_uid's gid
113  * > 0 : owner = new_uid, group = abrt group's gid
114  *
115  * On success, zero is returned. On error, -1 is returned.
116  */
117 int dd_chown(struct dump_dir *dd, uid_t new_uid);
118 
119 
120 /* reported_to handling */
121 #define add_reported_to_data libreport_add_reported_to_data
122 int add_reported_to_data(char **reported_to, const char *line);
123 #define add_reported_to libreport_add_reported_to
124 void add_reported_to(struct dump_dir *dd, const char *line);
126  char *label;
127  char *url;
128  char *msg;
129  char *bthash;
130  /* char *whole_line; */
131  /* time_t timestamp; */
132  /* ^^^ if you add more fields, don't forget to update free_report_result() */
133 };
134 typedef struct report_result report_result_t;
135 #define free_report_result libreport_free_report_result
136 void free_report_result(struct report_result *result);
137 #define find_in_reported_to_data libreport_find_in_reported_to_data
138 report_result_t *find_in_reported_to_data(const char *reported_to, const char *report_label);
139 #define find_in_reported_to libreport_find_in_reported_to
140 report_result_t *find_in_reported_to(struct dump_dir *dd, const char *report_label);
141 #define read_entire_reported_to_data libreport_read_entire_reported_to_data
142 GList *read_entire_reported_to_data(const char* reported_to);
143 #define read_entire_reported_to libreport_read_entire_reported_to
144 GList *read_entire_reported_to(struct dump_dir *dd);
145 
146 
147 void delete_dump_dir(const char *dirname);
148 /* Checks dump dir accessibility for particular uid.
149  *
150  * If the directory doesn't exist the directory is not accessible and errno is
151  * set to ENOTDIR.
152  *
153  * Returns non zero if dump dir is accessible otherwise return 0 value.
154  */
155 int dump_dir_accessible_by_uid(const char *dirname, uid_t uid);
156 int fdump_dir_accessible_by_uid(int dir_fd, uid_t uid);
157 
158 enum {
159  DD_STAT_ACCESSIBLE_BY_UID = 1,
160  DD_STAT_OWNED_BY_UID = DD_STAT_ACCESSIBLE_BY_UID << 1,
161 };
162 
163 /* Gets information about a dump directory for particular uid.
164  *
165  * If the directory doesn't exist the directory is not accessible and errno is
166  * set to ENOTDIR.
167  *
168  * Returns negative number if error occurred otherwise returns 0 or positive number.
169  */
170 int dump_dir_stat_for_uid(const char *dirname, uid_t uid);
171 int fdump_dir_stat_for_uid(int dir_fd, uid_t uid);
172 
173 /* creates not_reportable file in the problem directory and saves the
174  reason to it, which prevents libreport from reporting the problem
175  On success, zero is returned.
176  On error, -1 is returned and an error message is logged.
177  - this could probably happen only if the dump dir is not locked
178 */
179 int dd_mark_as_notreportable(struct dump_dir *dd, const char *reason);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 
185 #endif