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");
50 * Returns an open FILE pointer to the config file;
51 * on error, NULL is returned.
53 static FILE *open_config(char *path
, const char *mode
)
58 file_path
= config_get_file_path(path
);
59 if (file_path
== NULL
) {
63 fp
= fopen(file_path
, mode
);
74 * Creates the empty config file at the path.
75 * On success, returns 0;
76 * on error, returns -1.
78 static int create_config_file(char *path
)
83 fp
= open_config(path
, "w+");
85 ERR("Unable to create config file");
97 * Append data to the config file in file_path
98 * On success, returns 0;
99 * on error, returns -1.
101 static int write_config(char *file_path
, size_t size
, char *data
)
107 fp
= open_config(file_path
, "a");
113 /* Write session name into config file */
114 len
= fwrite(data
, size
, 1, fp
);
119 PERROR("close write_config");
126 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
128 char *config_get_default_path(void)
130 return getenv("HOME");
134 * Destroys directory config and file config.
136 void config_destroy(char *path
)
141 config_path
= config_get_file_path(path
);
142 if (config_path
== NULL
) {
146 if (!config_exists(config_path
)) {
150 DBG("Removing %s\n", config_path
);
151 ret
= remove(config_path
);
153 perror("remove config file");
160 * Destroys the default config
162 void config_destroy_default(void)
164 char *path
= config_get_default_path();
168 config_destroy(path
);
172 * Returns 1 if config exists, 0 otherwise
174 int config_exists(const char *path
)
179 ret
= stat(path
, &info
);
183 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
187 * Returns the session name from the config file.
188 * The caller is responsible for freeing the returned string.
189 * On error, NULL is returned.
191 char *config_read_session_name(char *path
)
195 char var
[NAME_MAX
], *session_name
;
197 session_name
= malloc(NAME_MAX
);
198 if (session_name
== NULL
) {
199 ERR("Out of memory");
203 fp
= open_config(path
, "r");
205 ERR("Can't find valid lttng config %s/.lttngrc", path
);
206 MSG("Did you create a session? (lttng create <my_session>)");
212 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
214 ERR("Missing session=NAME in config file.");
220 if (strcmp(var
, "session") == 0) {
229 PERROR("close config read session name");
238 PERROR("close config read session name found");
245 * Write session name option to the config file.
246 * On success, returns 0;
247 * on error, returns -1.
249 int config_add_session_name(char *path
, char *name
)
252 char *attr
= "session=";
253 /* Max name len accepted plus attribute's len and the NULL byte. */
254 char session_name
[NAME_MAX
+ strlen(attr
) + 1];
257 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
258 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
260 ret
= snprintf(session_name
, sizeof(session_name
), "%s%s\n", attr
, name
);
265 ret
= write_config(path
, ret
, session_name
);
271 * Init configuration directory and file.
272 * On success, returns 0;
273 * on error, returns -1.
275 int config_init(char *session_name
)
280 path
= config_get_default_path();
286 /* Create default config file */
287 ret
= create_config_file(path
);
292 ret
= config_add_session_name(path
, session_name
);
297 DBG("Init config session in %s", path
);
This page took 0.050526 seconds and 4 git commands to generate.