blob: 1e375fbcce460b7c8e9f76d515b1d4a219e8289b [file] [log] [blame]
Petr Machata41498912012-12-04 17:57:34 +01001/*
2 * This file is part of ltrace.
Petr Machata33f0ca52013-01-07 19:42:13 +01003 * Copyright (C) 2012, 2013 Petr Machata
Petr Machata41498912012-12-04 17:57:34 +01004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 */
20
21#define _POSIX_C_SOURCE 200809L
22#include <sys/types.h>
23#include <alloca.h>
24#include <errno.h>
25#include <pwd.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
Petr Machatab3d61802013-10-16 17:02:20 +020031#include "backend.h"
Petr Machata41498912012-12-04 17:57:34 +010032#include "dict.h"
33#include "options.h"
Petr Machatab3d61802013-10-16 17:02:20 +020034#include "sysdep.h"
Petr Machatab3d61802013-10-16 17:02:20 +020035#include "vect.h"
Petr Machata41498912012-12-04 17:57:34 +010036
37static char *
38append(const char *str1, const char *str2)
39{
40 char *ret = malloc(strlen(str1) + strlen(str2) + 2);
41 if (ret == NULL)
42 return ret;
43 strcpy(stpcpy(ret, str1), str2);
44 return ret;
45}
46
47static void
48add_dir(struct vect *dirs, const char *str1, const char *str2)
49{
50 char *dir = append(str1, str2);
51 if (dir != NULL
52 && VECT_PUSHBACK(dirs, &dir) < 0)
53 fprintf(stderr,
54 "Couldn't store candidate config directory %s%s: %s.\n",
55 str1, str2, strerror(errno));
56}
57
58static enum callback_status
59add_dir_component_cb(struct opt_F_t *entry, void *data)
60{
61 struct vect *dirs = data;
62 if (opt_F_get_kind(entry) == OPT_F_DIR)
63 add_dir(dirs, entry->pathname, "/ltrace");
64 return CBS_CONT;
65}
66
67static void
68destroy_opt_F_cb(struct opt_F_t *entry, void *data)
69{
70 opt_F_destroy(entry);
71}
72
Petr Machata364753a2012-12-06 18:28:17 +010073static char *g_home_dir = NULL;
74
Petr Machata41498912012-12-04 17:57:34 +010075int
76os_get_config_dirs(int private, const char ***retp)
77{
78 /* Vector of char *. Contains first pointers to local paths,
79 * then NULL, then pointers to system paths, then another
80 * NULL. SYS_START points to the beginning of the second
81 * part. */
82 static struct vect dirs;
83 static ssize_t sys_start = 0;
84
85again:
86 if (sys_start != 0) {
87 if (sys_start == -1)
88 return -1;
89
Petr Machata364753a2012-12-06 18:28:17 +010090 if (retp != NULL) {
91 if (private)
92 *retp = VECT_ELEMENT(&dirs, const char *, 0);
93 else
94 *retp = VECT_ELEMENT(&dirs, const char *,
95 (size_t)sys_start);
96 }
97
Petr Machata41498912012-12-04 17:57:34 +010098 return 0;
99 }
100
101 VECT_INIT(&dirs, char *);
102
103 char *home = getenv("HOME");
104 if (home == NULL) {
105 struct passwd *pwd = getpwuid(getuid());
106 if (pwd != NULL)
107 home = pwd->pw_dir;
108 }
Petr Machata364753a2012-12-06 18:28:17 +0100109
Petr Machata33f0ca52013-01-07 19:42:13 +0100110 size_t home_len = home != NULL ? strlen(home) : 0;
111
Petr Machata41498912012-12-04 17:57:34 +0100112 /* The values coming from getenv and getpwuid may not be
113 * persistent. */
Petr Machata364753a2012-12-06 18:28:17 +0100114 if (home != NULL) {
115 g_home_dir = strdup(home);
116 if (g_home_dir != NULL) {
117 home = g_home_dir;
118 } else {
Petr Machata33f0ca52013-01-07 19:42:13 +0100119 char *tmp = alloca(home_len + 1);
Petr Machata364753a2012-12-06 18:28:17 +0100120 strcpy(tmp, home);
121 home = tmp;
122 }
Petr Machata41498912012-12-04 17:57:34 +0100123 }
124
125 char *xdg_home = getenv("XDG_CONFIG_HOME");
126 if (xdg_home == NULL && home != NULL) {
Petr Machata33f0ca52013-01-07 19:42:13 +0100127 xdg_home = alloca(home_len + sizeof "/.config");
128 sprintf(xdg_home, "%s/.config", home);
Petr Machata41498912012-12-04 17:57:34 +0100129 }
130 if (xdg_home != NULL)
131 add_dir(&dirs, xdg_home, "/ltrace");
132 if (home != NULL)
133 add_dir(&dirs, home, "/.ltrace");
134
135 char *delim = NULL;
136 if (VECT_PUSHBACK(&dirs, &delim) < 0) {
137 fail:
138 /* This can't work :( */
139 fprintf(stderr,
140 "Couldn't initialize list of config directories: %s.\n",
141 strerror(errno));
142 VECT_DESTROY(&dirs, const char *, dict_dtor_string, NULL);
143 sys_start = -1;
144 return -1;
145 }
146 sys_start = vect_size(&dirs);
147
148 /* """preference-ordered set of base directories to search for
149 * configuration files in addition to the $XDG_CONFIG_HOME
150 * base directory. The directories in $XDG_CONFIG_DIRS should
151 * be seperated with a colon ':'.""" */
152 char *xdg_sys = getenv("XDG_CONFIG_DIRS");
153 if (xdg_sys != NULL) {
154 struct vect v;
155 VECT_INIT(&v, struct opt_F_t);
156 if (parse_colon_separated_list(xdg_sys, &v) < 0
157 || VECT_EACH(&v, struct opt_F_t, NULL,
158 add_dir_component_cb, &dirs) != NULL)
159 fprintf(stderr,
160 "Error processing $XDG_CONFIG_DIRS '%s': %s\n",
161 xdg_sys, strerror(errno));
162 VECT_DESTROY(&v, struct opt_F_t, destroy_opt_F_cb, NULL);
163 }
164
Petr Machataaa3db6b2013-10-23 00:52:13 +0200165 /* PKGDATADIR is passed via -D when compiling. */
166 const char *pkgdatadir = PKGDATADIR;
167 if (pkgdatadir != NULL)
168 add_dir(&dirs, pkgdatadir, "");
Petr Machata41498912012-12-04 17:57:34 +0100169
170 if (VECT_PUSHBACK(&dirs, &delim) < 0)
171 goto fail;
172
173 goto again;
174}
Petr Machata364753a2012-12-06 18:28:17 +0100175
176int
Petr Machataaa3db6b2013-10-23 00:52:13 +0200177os_get_ltrace_conf_filenames(struct vect *retp)
Petr Machata364753a2012-12-06 18:28:17 +0100178{
Petr Machataaa3db6b2013-10-23 00:52:13 +0200179 char *homepath = NULL;
180 char *syspath = NULL;
181
182#define FN ".ltrace.conf"
Petr Machata364753a2012-12-06 18:28:17 +0100183 if (g_home_dir == NULL)
184 os_get_config_dirs(0, NULL);
Petr Machataaa3db6b2013-10-23 00:52:13 +0200185
186 if (g_home_dir != NULL) {
187 homepath = malloc(strlen(g_home_dir) + 1 + sizeof FN);
188 if (homepath == NULL
189 || sprintf(homepath, "%s/%s", g_home_dir, FN) < 0) {
190 fail:
191 free(syspath);
192 free(homepath);
193 return -1;
194 }
195 }
196
197 /* SYSCONFDIR is passed via -D when compiling. */
198 const char *sysconfdir = SYSCONFDIR;
199 if (sysconfdir != NULL && *sysconfdir != '\0') {
200 /* No +1, we skip the initial period. */
201 syspath = malloc(strlen(sysconfdir) + sizeof FN);
202 if (syspath == NULL
203 || sprintf(syspath, "%s/%s", sysconfdir, FN + 1) < 0)
204 goto fail;
205 }
206
207 if (VECT_PUSHBACK(retp, &homepath) < 0
208 || VECT_PUSHBACK(retp, &syspath) < 0)
209 goto fail;
210
211 return 0;
Petr Machata364753a2012-12-06 18:28:17 +0100212}