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