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 * Returns the path with '/CONFIG_FILENAME' added to it;
33 * path will be NULL if an error occurs.
35 char *config_get_file_path(char *path
)
40 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
42 ERR("Fail allocating config file path");
49 * Returns an open FILE pointer to the config file;
50 * on error, NULL is returned.
52 static FILE *open_config(char *path
, const char *mode
)
57 file_path
= config_get_file_path(path
);
58 if (file_path
== NULL
) {
62 fp
= fopen(file_path
, mode
);
73 * Creates the empty config file at the path.
74 * On success, returns 0;
75 * on error, returns -1.
77 static int create_config_file(char *path
)
82 fp
= open_config(path
, "w+");
84 ERR("Unable to create config file");
96 * Append data to the config file in file_path
97 * On success, returns 0;
98 * on error, returns -1.
100 static int write_config(char *file_path
, size_t size
, char *data
)
106 fp
= open_config(file_path
, "a");
112 /* Write session name into config file */
113 len
= fwrite(data
, size
, 1, fp
);
118 PERROR("close write_config");
125 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
127 char *config_get_default_path(void)
129 return getenv("HOME");
133 * Destroys directory config and file config.
135 void config_destroy(char *path
)
140 config_path
= config_get_file_path(path
);
141 if (config_path
== NULL
) {
145 if (!config_exists(config_path
)) {
149 DBG("Removing %s\n", config_path
);
150 ret
= remove(config_path
);
152 perror("remove config file");
159 * Destroys the default config
161 void config_destroy_default(void)
163 char *path
= config_get_default_path();
167 config_destroy(path
);
171 * Returns 1 if config exists, 0 otherwise
173 int config_exists(const char *path
)
178 ret
= stat(path
, &info
);
182 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
186 * Returns the session name from the config file.
187 * The caller is responsible for freeing the returned string.
188 * On error, NULL is returned.
190 char *config_read_session_name(char *path
)
194 char var
[NAME_MAX
], *session_name
;
196 session_name
= malloc(NAME_MAX
);
197 if (session_name
== NULL
) {
198 ERR("Out of memory");
202 fp
= open_config(path
, "r");
204 ERR("Can't find valid lttng config %s/.lttngrc", path
);
205 MSG("Did you create a session? (lttng create <my_session>)");
211 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
213 ERR("Missing session=NAME in config file.");
219 if (strcmp(var
, "session") == 0) {
228 PERROR("close config read session name");
237 PERROR("close config read session name found");
244 * Write session name option to the config file.
245 * On success, returns 0;
246 * on error, returns -1.
248 int config_add_session_name(char *path
, char *name
)
251 char session_name
[NAME_MAX
];
254 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
255 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
257 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
258 if ((ret
< 0) || (ret
>= NAME_MAX
)) {
262 ret
= write_config(path
, ret
, session_name
);
268 * Init configuration directory and file.
269 * On success, returns 0;
270 * on error, returns -1.
272 int config_init(char *session_name
)
277 path
= config_get_default_path();
283 /* Create default config file */
284 ret
= create_config_file(path
);
289 ret
= config_add_session_name(path
, session_name
);
294 DBG("Init config session in %s", path
);
This page took 0.036538 seconds and 5 git commands to generate.