00991b08195319f58f3d5489ff10ed679204f8aa
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 as published by
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
28 #include <common/error.h>
33 * config_get_file_path
35 * Returns the path with '/CONFIG_FILENAME' added to it;
36 * path will be NULL if an error occurs.
38 char *config_get_file_path(char *path
)
43 ret
= asprintf(&file_path
, "%s/%s", path
, CONFIG_FILENAME
);
45 ERR("Fail allocating config file path");
54 * Returns an open FILE pointer to the config file;
55 * on error, NULL is returned.
57 static FILE *open_config(char *path
, const char *mode
)
62 file_path
= config_get_file_path(path
);
63 if (file_path
== NULL
) {
67 fp
= fopen(file_path
, mode
);
82 * Creates the empty config file at the path.
83 * On success, returns 0;
84 * on error, returns -1.
86 static int create_config_file(char *path
)
91 fp
= open_config(path
, "w+");
93 ERR("Unable to create config file");
107 * Append data to the config file in file_path
108 * On success, returns 0;
109 * on error, returns -1.
111 static int write_config(char *file_path
, size_t size
, char *data
)
117 fp
= open_config(file_path
, "a");
123 /* Write session name into config file */
124 len
= fwrite(data
, size
, 1, fp
);
134 * config_get_default_path
136 * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer.
138 char *config_get_default_path(void)
140 return getenv("HOME");
146 * Destroys directory config and file config.
148 void config_destroy(char *path
)
153 config_path
= config_get_file_path(path
);
154 if (config_path
== NULL
) {
158 ret
= remove(config_path
);
160 perror("remove config file");
167 * config_read_session_name
169 * Returns the session name from the config file.
170 * The caller is responsible for freeing the returned string.
171 * On error, NULL is returned.
173 char *config_read_session_name(char *path
)
177 char var
[NAME_MAX
], *session_name
;
179 session_name
= malloc(NAME_MAX
);
180 if (session_name
== NULL
) {
181 ERR("Out of memory");
185 fp
= open_config(path
, "r");
187 ERR("Can't find valid lttng config %s/.lttngrc", path
);
188 MSG("Did you create a session? (lttng create <my_session>)");
193 if ((ret
= fscanf(fp
, "%[^'=']=%s\n", var
, session_name
)) != 2) {
195 ERR("Missing session=NAME in config file.");
201 if (strcmp(var
, "session") == 0) {
219 * config_add_session_name
221 * Write session name option to the config file.
222 * On success, returns 0;
223 * on error, returns -1.
225 int config_add_session_name(char *path
, char *name
)
228 char session_name
[NAME_MAX
];
231 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
232 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
234 ret
= snprintf(session_name
, NAME_MAX
, "session=%s\n", name
);
235 if ((ret
< 0) || (ret
>= NAME_MAX
)) {
239 ret
= write_config(path
, ret
, session_name
);
247 * Init configuration directory and file.
248 * On success, returns 0;
249 * on error, returns -1.
251 int config_init(char *session_name
)
256 path
= config_get_default_path();
262 /* Create default config file */
263 ret
= create_config_file(path
);
268 ret
= config_add_session_name(path
, session_name
);
273 DBG("Init config session in %s", path
);
This page took 0.03624 seconds and 5 git commands to generate.