50af2289d11f0d83fd636cb136e504b3887c9be3
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
27 #include <common/error.h>
32 * config_get_file_path
34 * Returns the path with '/CONFIG_FILENAME' added to it;
35 * path will be NULL if an error occurs.
37 char *config_get_file_path(char *path
)
42 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
44 ERR("Fail allocating config file path");
53 * Returns an open FILE pointer to the config file;
54 * on error, NULL is returned.
56 static FILE *open_config(char *path
, const char *mode
)
61 file_path
= config_get_file_path(path
);
62 if (file_path
== NULL
) {
66 fp
= fopen(file_path
, mode
);
81 * Creates the empty config file at the path.
82 * On success, returns 0;
83 * on error, returns -1.
85 static int create_config_file(char *path
)
90 fp
= open_config(path
, "w+");
92 ERR("Unable to create config file");
106 * Append data to the config file in file_path
107 * On success, returns 0;
108 * on error, returns -1.
110 static int write_config(char *file_path
, size_t size
, char *data
)
116 fp
= open_config(file_path
, "a");
122 /* Write session name into config file */
123 len
= fwrite(data
, size
, 1, fp
);
133 * config_get_default_path
135 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
137 char *config_get_default_path(void)
139 return getenv("HOME");
145 * Destroys directory config and file config.
147 void config_destroy(char *path
)
152 config_path
= config_get_file_path(path
);
153 if (config_path
== NULL
) {
157 if (!config_exists(config_path
)) {
161 DBG("Removing %s\n", config_path
);
162 ret
= remove(config_path
);
164 perror("remove config file");
171 * config_destroy_default
173 * Destroys the default config
176 void config_destroy_default(void)
178 char *path
= config_get_default_path();
182 config_destroy(path
);
188 * Returns 1 if config exists, 0 otherwise
190 int config_exists(const char *path
)
195 ret
= stat(path
, &info
);
199 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
203 * config_read_session_name
205 * Returns the session name from the config file.
206 * The caller is responsible for freeing the returned string.
207 * On error, NULL is returned.
209 char *config_read_session_name(char *path
)
213 char var
[NAME_MAX
], *session_name
;
215 session_name
= malloc(NAME_MAX
);
216 if (session_name
== NULL
) {
217 ERR("Out of memory");
221 fp
= open_config(path
, "r");
223 ERR("Can't find valid lttng config %s/.lttngrc", path
);
224 MSG("Did you create a session? (lttng create <my_session>)");
229 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
231 ERR("Missing session=NAME in config file.");
237 if (strcmp(var
, "session") == 0) {
255 * config_add_session_name
257 * Write session name option to the config file.
258 * On success, returns 0;
259 * on error, returns -1.
261 int config_add_session_name(char *path
, char *name
)
264 char session_name
[NAME_MAX
];
267 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
268 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
270 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
271 if ((ret
< 0) || (ret
>= NAME_MAX
)) {
275 ret
= write_config(path
, ret
, session_name
);
283 * Init configuration directory and file.
284 * On success, returns 0;
285 * on error, returns -1.
287 int config_init(char *session_name
)
292 path
= config_get_default_path();
298 /* Create default config file */
299 ret
= create_config_file(path
);
304 ret
= config_add_session_name(path
, session_name
);
309 DBG("Init config session in %s", path
);
This page took 0.038646 seconds and 5 git commands to generate.