~ubuntu-core-dev/ubuntu/raring/upstart/raring

« back to all changes in this revision

Viewing changes to init/xdg.c

  • Committer: Stéphane Graber
  • Date: 2013-03-07 18:43:01 UTC
  • mfrom: (1182.56.54 upstart)
  • Revision ID: stgraber@ubuntu.com-20130307184301-dlmb1c5bwonqagkw
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* upstart
 
2
 *
 
3
 * xdg.c - XDG compliant path constructor
 
4
 *
 
5
 * Copyright © 2012 Canonical Ltd.
 
6
 * Author: Dmitrijs Ledkovs <dmitrijs.ledkovs@canonical.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify
 
9
 * it under the terms of the GNU General Public License version 2, as
 
10
 * published by the Free Software Foundation.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License along
 
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
# include <config.h>
 
24
#endif /* HAVE_CONFIG_H */
 
25
 
 
26
#include <stdlib.h>
 
27
#include <sys/stat.h>
 
28
#include <sys/types.h>
 
29
 
 
30
#include <nih/alloc.h>
 
31
#include <nih/logging.h>
 
32
#include <nih/string.h>
 
33
 
 
34
#include "paths.h"
 
35
#include "xdg.h"
 
36
 
 
37
#ifndef INITCTL_BUILD
 
38
 
 
39
/**
 
40
 * user_mode:
 
41
 *
 
42
 * If TRUE, upstart runs in user session mode.
 
43
 **/
 
44
int user_mode = FALSE;
 
45
 
 
46
/**
 
47
 * session_file:
 
48
 *
 
49
 * Full path to file containing UPSTART_SESSION details (only set when
 
50
 * user_mode in operation).
 
51
 *
 
52
 * File is created on startup and removed on clean shutdown.
 
53
 **/
 
54
const char *session_file = NULL;
 
55
 
 
56
#endif /* INITCTL_BUILD */
 
57
 
 
58
/**
 
59
 * get_subdir:
 
60
 * @dir: initial directory,
 
61
 * @suffix: sub-directory of @dir,
 
62
 * @create: flag to create sub-directory.
 
63
 * 
 
64
 * Construct path by appending @suffix to @dir. If @create
 
65
 * flag is TRUE, also attempt to create that directory.
 
66
 *
 
67
 * Errors upon directory creation are ignored.
 
68
 * 
 
69
 * Returns: Newly-allocated path, or NULL on error.
 
70
 **/
 
71
char *
 
72
get_subdir (const char *dir, const char *suffix, int create)
 
73
{
 
74
        char *newdir;
 
75
 
 
76
        nih_assert (dir != NULL);
 
77
        nih_assert (suffix != NULL);
 
78
        nih_assert (suffix[0]);
 
79
        
 
80
        if (dir[0] == '/') {
 
81
                newdir = nih_sprintf (NULL, "%s/%s", dir, suffix);
 
82
                if (! newdir)
 
83
                        return NULL;
 
84
                if (create)
 
85
                        mkdir (newdir, INIT_XDG_PATH_MODE);
 
86
                return newdir;
 
87
        }
 
88
 
 
89
        return NULL;
 
90
}
 
91
 
 
92
/**
 
93
 * get_home_subdir:
 
94
 *
 
95
 * @suffix: sub-directory name,
 
96
 * @create: flag to create sub-directory.
 
97
 *
 
98
 * Construct path to @suffix directory in user's HOME directory.
 
99
 * If @create is TRUE, also attempt to create that directory.
 
100
 *
 
101
 * Errors upon directory creation are ignored.
 
102
 * 
 
103
 * Returns: Newly-allocated path, or NULL on error.
 
104
 **/
 
105
char *
 
106
get_home_subdir (const char *suffix, int create)
 
107
{
 
108
        char *env;
 
109
 
 
110
        env = getenv ("HOME");
 
111
        if (! env)
 
112
                return NULL;
 
113
 
 
114
        return get_subdir (env, suffix, create);
 
115
}
 
116
 
 
117
/**
 
118
 * xdg_get_cache_home:
 
119
 *
 
120
 * Determine an XDG compliant XDG_CACHE_HOME
 
121
 *
 
122
 * Returns: newly-allocated path, or NULL on error.
 
123
 **/
 
124
char *
 
125
xdg_get_cache_home (void)
 
126
{
 
127
        char             *dir;
 
128
 
 
129
        dir = getenv ("XDG_CACHE_HOME");
 
130
        
 
131
        if (dir && dir[0] == '/') {
 
132
                mkdir (dir, INIT_XDG_PATH_MODE);
 
133
                dir = nih_strdup (NULL, dir);
 
134
                return dir;
 
135
        }
 
136
 
 
137
        /* Per XDG spec, we should only create dirs, if we are
 
138
         * attempting to write and the dir is not there. Here we
 
139
         * anticipate logging to happen really soon now, hence we
 
140
         * pre-create the cache dir. That does not protect us from
 
141
         * this directory disappering while upstart is running. =/
 
142
         * hence this dir should be created each time we try to write
 
143
         * log... */
 
144
        dir = get_home_subdir (".cache", TRUE);
 
145
 
 
146
        return dir;
 
147
}
 
148
 
 
149
/**
 
150
 * xdg_get_config_home:
 
151
 *
 
152
 * Determine an XDG compliant XDG_CONFIG_HOME
 
153
 *
 
154
 * Returns: newly-allocated path, or NULL on error.
 
155
 **/
 
156
char *
 
157
xdg_get_config_home (void)
 
158
{
 
159
        char  *dir;
 
160
 
 
161
        dir = getenv ("XDG_CONFIG_HOME");
 
162
        
 
163
        if (dir && dir[0] == '/') {
 
164
                mkdir (dir, INIT_XDG_PATH_MODE);
 
165
                dir = nih_strdup (NULL, dir);
 
166
                return dir;
 
167
        }
 
168
 
 
169
        /* Per XDG spec, we should only create dirs, if we are
 
170
         * attempting to write to the dir. But we only read config
 
171
         * dir. But we rather create it, to place inotify watch on
 
172
         * it. */
 
173
        dir = get_home_subdir (".config", TRUE);
 
174
 
 
175
        return dir;
 
176
}
 
177
 
 
178
/**
 
179
 * xdg_get_runtime_dir:
 
180
 *
 
181
 * Determine an XDG compliant XDG_RUNTIME_DIR.
 
182
 *
 
183
 * Note: No attempt is made to create this directory since if it does
 
184
 * not exist, a non-priv user is unlikely be able to create it anyway.
 
185
 *
 
186
 * Returns: newly-allocated path, or NULL on error.
 
187
 **/
 
188
char *
 
189
xdg_get_runtime_dir (void)
 
190
{
 
191
        char *dir;
 
192
 
 
193
        dir = getenv ("XDG_RUNTIME_DIR");
 
194
 
 
195
        if (dir && dir[0] == '/')
 
196
                dir = nih_strdup (NULL, dir);
 
197
 
 
198
        return dir;
 
199
}
 
200
 
 
201
/**
 
202
 * get_session_dir:
 
203
 *
 
204
 * Determine full path to XDG-compliant session directory used to store
 
205
 * session files.
 
206
 *
 
207
 * Returns: Newly-allocated path, or NULL on error.
 
208
 **/
 
209
char *
 
210
get_session_dir (void)
 
211
{
 
212
        nih_local char  *runtime_dir = NULL;
 
213
        nih_local char  *dir = NULL;
 
214
        char            *session_dir;
 
215
 
 
216
        runtime_dir = xdg_get_runtime_dir ();
 
217
 
 
218
        if (runtime_dir && runtime_dir[0] == '/') {
 
219
                dir = get_subdir (runtime_dir, INIT_XDG_SUBDIR, TRUE);
 
220
                if (! dir)
 
221
                        return NULL;
 
222
 
 
223
                session_dir = get_subdir (dir, INIT_XDG_SESSION_SUBDIR,
 
224
                                TRUE);
 
225
 
 
226
                return session_dir;
 
227
        }
 
228
 
 
229
        return NULL;
 
230
}
 
231
 
 
232
/**
 
233
 * xdg_get_config_dirs:
 
234
 *
 
235
 * Determine a list of XDG compliant XDG_CONFIG_DIRS
 
236
 *
 
237
 * Returns: newly-allocated array of paths, or NULL on error.
 
238
 **/
 
239
char **
 
240
xdg_get_config_dirs (void)
 
241
{
 
242
        char         *env_path;
 
243
        char        **dirs = NULL;
 
244
 
 
245
        env_path = getenv ("XDG_CONFIG_DIRS");
 
246
        if (! env_path || ! env_path[0])
 
247
                env_path = "/etc/xdg";
 
248
 
 
249
        dirs = nih_str_split (NULL, env_path, ":", TRUE);
 
250
 
 
251
        return dirs;
 
252
}
 
253
 
 
254
/**
 
255
 * get_user_upstart_dirs:
 
256
 *
 
257
 * Construct an array of user session config source paths to config
 
258
 * dirs for a particular user. This array is sorted in highest
 
259
 * priority order and therefore can be iterated to add each of these
 
260
 * directories as config source dirs, when e.g. upstart is running as
 
261
 * user session init.
 
262
 *
 
263
 * Returns: newly-allocated array of paths, or NULL or error.
 
264
 **/
 
265
char **
 
266
get_user_upstart_dirs (void)
 
267
{
 
268
        char       *path = NULL;
 
269
        char      **dirs = NULL;
 
270
        char  **all_dirs = NULL;
 
271
 
 
272
        all_dirs = nih_str_array_new (NULL);
 
273
        if (! all_dirs)
 
274
                goto error;
 
275
 
 
276
        /* The current order is inline with Enhanced User Sessions Spec */
 
277
 
 
278
        /* User's: ~/.config/upstart or XDG_CONFIG_HOME/upstart */
 
279
        path = xdg_get_config_home ();
 
280
        if (! path)
 
281
                goto error;
 
282
 
 
283
        if (path && path[0]) {
 
284
                if (! nih_strcat_sprintf (&path, NULL, "/%s", INIT_XDG_SUBDIR))
 
285
                        goto error;
 
286
                mkdir (path, INIT_XDG_PATH_MODE);
 
287
                if (! nih_str_array_add (&all_dirs, NULL, NULL, path))
 
288
                        goto error;
 
289
                nih_free (path);
 
290
                path = NULL;
 
291
        }
 
292
 
 
293
        /* Legacy User's: ~/.init */
 
294
        path = get_home_subdir (USERCONFDIR, FALSE);
 
295
        if (! path)
 
296
                goto error;
 
297
 
 
298
        if (path && path[0]) {
 
299
                if (! nih_str_array_add (&all_dirs, NULL, NULL, path))
 
300
                        goto error;
 
301
                nih_free (path);
 
302
                path = NULL;
 
303
        }
 
304
 
 
305
        /* Systems': XDG_CONFIG_DIRS/upstart */
 
306
        dirs = xdg_get_config_dirs ();
 
307
        if (! dirs)
 
308
                goto error;
 
309
 
 
310
        for (char **p = dirs; p && *p; p++) {
 
311
                if (*p[0] != '/')
 
312
                        continue;
 
313
                if (! nih_strcat_sprintf (p, NULL, "/%s", INIT_XDG_SUBDIR))
 
314
                        goto error;
 
315
                if (! nih_str_array_add (&all_dirs, NULL, NULL, *p))
 
316
                        goto error;
 
317
        }
 
318
        nih_free (dirs);
 
319
        dirs = NULL;
 
320
 
 
321
        /* System's read-only location */
 
322
        if (! nih_str_array_add (&all_dirs, NULL, NULL, SYSTEM_USERCONFDIR))
 
323
                goto error;
 
324
 
 
325
        return all_dirs;
 
326
        
 
327
error:
 
328
        if (path)
 
329
                nih_free (path);
 
330
 
 
331
        if (dirs)
 
332
                nih_free (dirs);
 
333
 
 
334
        if (all_dirs)
 
335
                nih_free (all_dirs);
 
336
 
 
337
        return NULL;
 
338
}
 
339
 
 
340
/**
 
341
 * get_user_log_dir:
 
342
 *
 
343
 * Constructs an XDG compliant path to a cache directory in the user's
 
344
 * home directory. It can be used to store logs.
 
345
 *
 
346
 * Returns: newly-allocated array of paths, or NULL or error.
 
347
 **/
 
348
char *
 
349
get_user_log_dir (void)
 
350
{
 
351
        nih_local char *path = NULL;
 
352
        char *dir = NULL;
 
353
        path = xdg_get_cache_home ();
 
354
        if (path && path[0] == '/') {
 
355
                dir = nih_sprintf (NULL, "%s/%s", path, INIT_XDG_SUBDIR);
 
356
                if (! dir)
 
357
                        return NULL;
 
358
                mkdir (dir, INIT_XDG_PATH_MODE);
 
359
                return dir;
 
360
        }
 
361
        return NULL;
 
362
}