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.
25 #include <sys/types.h>
28 #include <common/common.h>
29 #include <common/utils.h>
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");
52 * Returns an open FILE pointer to the config file;
53 * on error, NULL is returned.
55 static FILE *open_config(char *path
, const char *mode
)
60 file_path
= config_get_file_path(path
);
61 if (file_path
== NULL
) {
65 fp
= fopen(file_path
, mode
);
76 * Creates the empty config file at the path.
77 * On success, returns 0;
78 * on error, returns -1.
80 static int create_config_file(char *path
)
85 fp
= open_config(path
, "w+");
87 ERR("Unable to create config file");
99 * Append data to the config file in file_path
100 * On success, returns 0;
101 * on error, returns -1.
103 static int write_config(char *file_path
, size_t size
, char *data
)
109 fp
= open_config(file_path
, "a");
115 /* Write session name into config file */
116 len
= fwrite(data
, size
, 1, fp
);
121 PERROR("close write_config");
128 * Destroys directory config and file config.
130 void config_destroy(char *path
)
135 config_path
= config_get_file_path(path
);
136 if (config_path
== NULL
) {
140 if (!config_exists(config_path
)) {
144 DBG("Removing %s\n", config_path
);
145 ret
= remove(config_path
);
147 PERROR("remove config file");
154 * Destroys the default config
156 void config_destroy_default(void)
158 char *path
= utils_get_home_dir();
162 config_destroy(path
);
166 * Returns 1 if config exists, 0 otherwise
168 int config_exists(const char *path
)
173 ret
= stat(path
, &info
);
177 return S_ISREG(info
.st_mode
) || S_ISDIR(info
.st_mode
);
181 * Returns the session name from the config file.
182 * The caller is responsible for freeing the returned string.
183 * On error, NULL is returned.
185 char *config_read_session_name(char *path
)
189 char var
[NAME_MAX
], *session_name
;
190 #if (NAME_MAX == 255)
191 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
194 session_name
= zmalloc(NAME_MAX
);
195 if (session_name
== NULL
) {
196 ERR("Out of memory");
200 fp
= open_config(path
, "r");
202 ERR("Can't find valid lttng config %s/.lttngrc", path
);
203 MSG("Did you create a session? (lttng create <my_session>)");
209 if ((ret
= fscanf(fp
, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
210 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API
"s\n",
211 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 *attr
= "session=";
252 /* Max name len accepted plus attribute's len and the NULL byte. */
253 char session_name
[NAME_MAX
+ strlen(attr
) + 1];
256 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
257 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
259 ret
= snprintf(session_name
, sizeof(session_name
), "%s%s\n", attr
, name
);
264 ret
= write_config(path
, ret
, session_name
);
270 * Init configuration directory and file.
271 * On success, returns 0;
272 * on error, returns -1.
274 int config_init(char *session_name
)
279 path
= utils_get_home_dir();
285 /* Create default config file */
286 ret
= create_config_file(path
);
291 ret
= config_add_session_name(path
, session_name
);
296 DBG("Init config session in %s", path
);