Merge authors: Dmitrijs Ledkovs (xnox) Related merge proposals: https://code.launchpad.net/~xnox/upstart/user-log-dir/+merge/143091 proposed by: Dmitrijs Ledkovs (xnox) review: Approve - James Hunt (jamesodhunt) ------------------------------------------------------------ revno: 1428 [merge] committer: James Hunt <[email protected]> branch nick: upstart timestamp: Wed 2013-01-23 13:56:17 +0000 message: * Merge of lp:~xnox/upstart/user-log-dir. modified: ChangeLog init/Makefile.am init/control.c init/job_process.c init/main.c init/man/init.5 init/man/init.8 init/tests/test_xdg.c init/xdg.c init/xdg.h
-- lp:upstart https://code.launchpad.net/~upstart-devel/upstart/trunk Your team Upstart Reviewers is subscribed to branch lp:upstart. To unsubscribe from this branch go to https://code.launchpad.net/~upstart-devel/upstart/trunk/+edit-subscription
=== modified file 'ChangeLog' --- ChangeLog 2013-01-21 16:24:48 +0000 +++ ChangeLog 2013-01-21 16:52:57 +0000 @@ -1,3 +1,15 @@ +2013-01-21 Dmitrijs Ledkovs <[email protected]> + + * init/xdg.[ch]: add xdg_get_cache_home and get_user_log_dir + functions. These retrieve XDG_CACHE_HOME and a subdir inside it + for upstart. + * init/tests/test_xdg.c: reuse test_get_config_home to test both + xdg_get_cache_home and xdg_get_config_home. Add test for + get_user_log_dir. + * init/main.c: use get_user_log_dir to setup logging + directory in user_mode. For now, command line argument is + honoured, while the environment override is not. + 2013-01-21 James Hunt <[email protected]> * init/log.c:log_clear_unflushed(): Simplify asserts. === modified file 'init/Makefile.am' --- init/Makefile.am 2012-12-19 12:46:46 +0000 +++ init/Makefile.am 2013-01-21 16:39:28 +0000 @@ -177,7 +177,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -191,7 +191,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -205,7 +205,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -219,7 +219,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -233,7 +233,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -247,7 +247,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -261,7 +261,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -275,7 +275,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -289,7 +289,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -303,7 +303,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -317,7 +317,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -331,7 +331,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -345,7 +345,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ @@ -366,7 +366,7 @@ system.o environ.o process.o \ job_class.o job_process.o job.o event.o event_operator.o blocked.o \ parse_job.o parse_conf.o conf.o control.o \ - session.o log.o state.o \ + session.o log.o state.o xdg.o \ com.ubuntu.Upstart.o \ com.ubuntu.Upstart.Job.o com.ubuntu.Upstart.Instance.o \ $(NIH_LIBS) \ === modified file 'init/control.c' --- init/control.c 2013-01-22 15:51:01 +0000 +++ init/control.c 2013-01-23 12:56:00 +0000 @@ -76,13 +76,6 @@ int use_session_bus = FALSE; /** - * user_mode: - * - * If TRUE, upstart runs in user session mode. - **/ -int user_mode = FALSE; - -/** * control_server_address: * * Address on which the control server may be reached. @@ -112,6 +105,8 @@ **/ NihList *control_conns = NULL; +/* External definitions */ +extern int user_mode; /** * control_init: === modified file 'init/job_process.c' --- init/job_process.c 2013-01-21 22:44:27 +0000 +++ init/job_process.c 2013-01-23 12:40:36 +0000 @@ -66,6 +66,7 @@ #include "job.h" #include "errors.h" #include "control.h" +#include "xdg.h" /** @@ -2197,7 +2198,7 @@ nih_assert (class->name); /* Override, primarily for tests */ - if (getenv (LOGDIR_ENV)) { + if (getenv (LOGDIR_ENV) && ! user_mode) { dir = nih_strdup (NULL, getenv (LOGDIR_ENV)); nih_debug ("Using alternative directory '%s' for logs", dir); } else { === modified file 'init/main.c' --- init/main.c 2013-01-22 15:53:05 +0000 +++ init/main.c 2013-01-23 12:40:36 +0000 @@ -949,6 +949,11 @@ if (log_dir) goto out; + if (user_mode) { + log_dir = get_user_log_dir (); + return; + } + log_dir = JOB_LOGDIR; dir = getenv (LOGDIR_ENV); === modified file 'init/man/init.5' --- init/man/init.5 2013-01-16 19:20:54 +0000 +++ init/man/init.5 2013-01-23 12:40:36 +0000 @@ -149,11 +149,8 @@ of that job name: any subsequently searched directory that contains a job of the same name will be ignored. The same applies for override files: only the first override file found in the search order will be -applied. Note that an override file does not have to be in the same -directory as the job it overrides, but if not in the directory of the -job it overrides, it must be in an -.I earlier -directory to that which contains the job. +applied. Note that an override file can be in the same directory or +earlier to that directory which contains the job file. Jobs in these locations are expected to launch the user's session. Upstart will try to parent all spawned process with the aid of @@ -166,6 +163,12 @@ When running in User Session mode, Upstart will kill all job processes on session logout or shutdown. +All log output will be in +.I $XDG_CACHE_HOME/upstart +which defaults to +.I $HOME/.cache/upstart +. + .\" .SS Configuration File Format Each line begins with a configuration stanza and continues until either @@ -664,19 +667,23 @@ .B log .RS .B -Only applies to system jobs: +Only applies to system and user session jobs: if specified by user jobs, the job will be considered to have specified the value .BR none "." -For system jobs, if \fBlog\fR is specified, standard input is connected +For system and user session jobs jobs, if \fBlog\fR is specified, standard input is connected to .IR /dev/null "," and standard output and standard error are connected to a pseudo-tty which logs all job output. Output is logged to file -.IR /var/log/upstart/<job-log-file> "." +.IR /var/log/upstart/<job-log-file> +or +.IR $XDG_CACHE_HOME/upstart/<job-log-file> +for system and user session jobs respectively. + If a job has specified \fBinstance\fR, .I <job-log-file> will equate to @@ -1019,18 +1026,30 @@ .TP .I $HOME/.init/*.conf User job configuration files. -Note that you may make the directory -.I $HOME/.init/ -a symbolic link to -.BR $HOME/.config/upstart/ "." . .TP .I $HOME/.init/*.override User job override files. . .TP +.I $XDG_CONFIG_HOME/upstart/*.conf +User session job configuration files. See +.B User Session Mode +for other locations. +. +.TP +.I $XDG_CONFIG_HOME/upstart/*.override +User session job override files. See +.B User Session Mode +for other locations. +. +.TP .I /var/log/upstart/*.log Default location of system job output logs. +. +.TP +.I $XDG_CACHE_HOME/upstart/*.log +Default location of user session job output logs. .RE .\" .SH AUTHOR === modified file 'init/man/init.8' --- init/man/init.8 2012-12-18 17:40:49 +0000 +++ init/man/init.8 2013-01-18 15:26:17 +0000 @@ -67,6 +67,8 @@ .B \-\-confdir \fIdirectory\fP Read job configuration files from a directory other than \fI/etc/init\fP. +For user session mode, read job configuration files from this +directory at highest priority. .\" .TP .B \-\-default-console \fIvalue\fP @@ -81,7 +83,8 @@ .TP .B \-\-logdir \fIdirectory\fP Write job output log files to a directory other than -\fI/var/log/upstart\fP. +\fI/var/log/upstart\fP (system mode) or \fI$XDG_CACHE_HOME/upstart\fP +(user session mode). .\" .TP .B \-\-no\-log === modified file 'init/tests/test_xdg.c' --- init/tests/test_xdg.c 2012-12-18 14:16:10 +0000 +++ init/tests/test_xdg.c 2013-01-22 12:01:56 +0000 @@ -22,12 +22,26 @@ #include <nih/string.h> #include <nih/test.h> +#include <limits.h> #include <stdlib.h> -#include <limits.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> #include "xdg.h" void +_test_dir_created (char * dirname) { + struct stat statbuf; + + TEST_EQ (lstat (dirname, &statbuf), 0); + TEST_TRUE (S_ISDIR (statbuf.st_mode)); + + /* Check that the created directory has 0700 permissions, as per XDG spec */ + TEST_EQ (0700, statbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)); +} + +void test_get_home_subdir (void) { char dirname[PATH_MAX]; @@ -40,7 +54,7 @@ TEST_EQ (unsetenv ("HOME"), 0); TEST_ALLOC_FAIL { - dir = get_home_subdir ("test"); + dir = get_home_subdir ("test", FALSE); TEST_EQ_P (dir, NULL); } @@ -54,7 +68,7 @@ expected = NIH_MUST (nih_sprintf (NULL, "%s/test", dirname)); } - dir = get_home_subdir ("test"); + dir = get_home_subdir ("test", FALSE); if (test_alloc_failed) { TEST_EQ_P (dir, NULL); @@ -69,28 +83,29 @@ } void -test_get_config_home (void) +_test_get_home (char * env_var_name, char * dir_name, char * (*function)(void)) { char dirname[PATH_MAX]; + char onemore[PATH_MAX]; char * outname; char * expected; - TEST_FUNCTION ("xdg_get_config_home"); - TEST_FEATURE ("with HOME set and without environment override"); TEST_FILENAME (dirname); TEST_EQ (setenv ("HOME", dirname, 1), 0); - TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0); + TEST_EQ (mkdir (dirname, 0755), 0); + TEST_EQ (unsetenv (env_var_name), 0); TEST_ALLOC_FAIL { TEST_ALLOC_SAFE { - expected = NIH_MUST (nih_sprintf (NULL, "%s/.config", dirname)); + expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name)); } outname = NULL; - outname = xdg_get_config_home (); + outname = function (); if (! test_alloc_failed) { TEST_EQ_STR (outname, expected); + _test_dir_created (expected); } else { TEST_EQ_P (outname, NULL); } @@ -98,83 +113,131 @@ if (outname) nih_free (outname); - nih_free(expected); + rmdir (expected); + nih_free (expected); } TEST_FEATURE ("with HOME set and with empty environment override"); - TEST_EQ (setenv ("XDG_CONFIG_HOME", "", 1), 0); - - - TEST_ALLOC_FAIL { - TEST_ALLOC_SAFE { - expected = NIH_MUST (nih_sprintf (NULL, "%s/.config", dirname)); - } - outname = NULL; - outname = xdg_get_config_home(); - - if (test_alloc_failed) { - TEST_EQ_P (outname, NULL); - } else { - TEST_EQ_STR (outname, expected); - } - if (outname) - nih_free (outname); - nih_free(expected); + TEST_EQ (setenv (env_var_name, "", 1), 0); + + + TEST_ALLOC_FAIL { + TEST_ALLOC_SAFE { + expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name)); + } + outname = NULL; + outname = function (); + + if (test_alloc_failed) { + TEST_EQ_P (outname, NULL); + } else { + TEST_EQ_STR (outname, expected); + _test_dir_created (expected); + } + if (outname) + nih_free (outname); + rmdir (expected); + nih_free (expected); + } + + TEST_FEATURE ("with HOME set and with relative environment override"); + TEST_EQ (setenv (env_var_name, "../", 1), 0); + + + TEST_ALLOC_FAIL { + TEST_ALLOC_SAFE { + expected = NIH_MUST (nih_sprintf (NULL, "%s/%s", dirname, dir_name)); + } + outname = NULL; + outname = function (); + + if (test_alloc_failed) { + TEST_EQ_P (outname, NULL); + } else { + TEST_EQ_STR (outname, expected); + _test_dir_created (expected); + } + if (outname) + nih_free (outname); + rmdir (expected); + nih_free (expected); } TEST_FEATURE ("with HOME set and with environment override"); - expected = NIH_MUST (nih_strdup (NULL, "/home/me/.config-test")); - TEST_EQ (setenv ("XDG_CONFIG_HOME", expected, 1), 0); + TEST_FILENAME (onemore); + expected = NIH_MUST (nih_sprintf (NULL, "%s", onemore)); + TEST_EQ (setenv (env_var_name, expected, 1), 0); TEST_ALLOC_FAIL { outname = NULL; - outname = xdg_get_config_home(); + outname = function (); if (test_alloc_failed) { TEST_EQ_P (outname, NULL); } else { TEST_EQ_STR (outname, expected); + _test_dir_created (expected); } if (outname) nih_free (outname); + rmdir (expected); } + TEST_EQ (rmdir (dirname), 0); + TEST_FEATURE ("without HOME set and with environment override"); TEST_EQ (unsetenv ("HOME"), 0); TEST_ALLOC_FAIL { outname = NULL; - outname = xdg_get_config_home(); + outname = function (); if (test_alloc_failed) { TEST_EQ_P (outname, NULL); } else { TEST_EQ_STR (outname, expected); + _test_dir_created (expected); } if (outname) nih_free (outname); + rmdir (expected); } nih_free(expected); TEST_FEATURE ("without HOME set and with empty environment override"); - TEST_EQ (setenv ("XDG_CONFIG_HOME", "", 1), 0); + TEST_EQ (setenv (env_var_name, "", 1), 0); TEST_ALLOC_FAIL { outname = NULL; - outname = xdg_get_config_home(); + outname = function (); TEST_EQ_P (outname, NULL); } TEST_FEATURE ("without HOME set and without environment override"); - TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0); + TEST_EQ (unsetenv (env_var_name), 0); TEST_ALLOC_FAIL { outname = NULL; - outname = xdg_get_config_home(); + outname = function (); TEST_EQ_P (outname, NULL); } } void +test_get_config_home (void) +{ + TEST_FUNCTION ("xdg_get_config_home"); + _test_get_home ("XDG_CONFIG_HOME", ".config", &xdg_get_config_home); + +} + +void +test_get_cache_home (void) +{ + TEST_FUNCTION ("xdg_get_cache_home"); + _test_get_home ("XDG_CACHE_HOME", ".cache", &xdg_get_cache_home); +} + +void test_get_config_dirs (void) { char **dirs = NULL; @@ -260,12 +323,12 @@ TEST_FEATURE ("with HOME set"); TEST_FILENAME (dirname); TEST_EQ (setenv ("HOME", dirname, 1), 0); + TEST_EQ (mkdir (dirname, 0755), 0); TEST_EQ (unsetenv ("XDG_CONFIG_HOME"), 0); TEST_EQ (unsetenv ("XDG_CONFIG_DIRS"), 0); TEST_ALLOC_FAIL { TEST_ALLOC_SAFE { - dirs = NULL; expected = nih_str_array_new (NULL); path = NIH_MUST (nih_sprintf (NULL, "%s/.config/upstart", dirname)); assert (nih_str_array_add (&expected, NULL, NULL, path)); @@ -282,15 +345,94 @@ TEST_EQ_P (dirs, NULL); } else { TEST_EQ_STR (dirs[0], expected[0]); + //_test_dir_created (expected[0]); TEST_EQ_STR (dirs[1], expected[1]); TEST_EQ_STR (dirs[2], "/etc/xdg/upstart"); TEST_EQ_STR (dirs[3], SYSTEM_USERCONFDIR); TEST_EQ (dirs[4], NULL); nih_free (dirs); } - nih_free(expected); - } - + TEST_ALLOC_SAFE { + rmdir (expected[0]); + path = nih_sprintf (NULL, "%s/.config", dirname); + rmdir (path); + nih_free (path); + nih_free (expected); + } + } + + TEST_FEATURE ("with some invalid XDG_CONFIG_DIRS"); + TEST_EQ (setenv ("XDG_CONFIG_DIRS", "/etc/xdg/xdg-subdir:../haha:/etc/xdg", 1), 0); + + TEST_ALLOC_FAIL { + TEST_ALLOC_SAFE { + expected = nih_str_array_new (NULL); + path = NIH_MUST (nih_sprintf (NULL, "%s/.config/upstart", dirname)); + assert (nih_str_array_add (&expected, NULL, NULL, path)); + nih_free(path); + path = NIH_MUST (nih_sprintf (NULL, "%s/.init", dirname)); + assert (nih_str_array_add (&expected, NULL, NULL, path)); + nih_free(path); + } + + dirs = NULL; + dirs = get_user_upstart_dirs (); + + if (test_alloc_failed) { + TEST_EQ_P (dirs, NULL); + } else { + TEST_EQ_STR (dirs[0], expected[0]); + _test_dir_created (expected[0]); + TEST_EQ_STR (dirs[1], expected[1]); + TEST_EQ_STR (dirs[2], "/etc/xdg/xdg-subdir/upstart"); + TEST_EQ_STR (dirs[3], "/etc/xdg/upstart"); + TEST_EQ_STR (dirs[4], SYSTEM_USERCONFDIR); + TEST_EQ (dirs[5], NULL); + nih_free (dirs); + } + TEST_ALLOC_SAFE { + rmdir (expected[0]); + path = nih_sprintf (NULL, "%s/.config", dirname); + rmdir (path); + nih_free (path); + nih_free (expected); + } + } + TEST_EQ (rmdir (dirname), 0); +} + +void +test_get_user_log_dir (void) +{ + char dirname[PATH_MAX]; + char *expected; + char *path; + + TEST_FUNCTION ("get_user_log_dir"); + TEST_FEATURE ("with HOME set"); + TEST_FILENAME (dirname); + TEST_EQ (setenv ("HOME", dirname, 1), 0); + TEST_EQ (mkdir (dirname, 0755), 0); + TEST_EQ (unsetenv ("XDG_CACHE_HOME"), 0); + + expected = nih_sprintf (NULL, "%s/.cache/upstart", dirname); + + TEST_ALLOC_FAIL { + path = get_user_log_dir (); + if (test_alloc_failed) { + TEST_EQ_P (path, NULL); + } else { + TEST_EQ_STR (path, expected); + _test_dir_created (expected); + nih_free (path); + } + } + rmdir (expected); + nih_free (expected); + path = nih_sprintf (NULL, "%s/.cache", dirname); + rmdir (path); + nih_free (path); + rmdir (dirname); } int @@ -301,6 +443,8 @@ test_get_config_home (); test_get_config_dirs (); test_get_user_upstart_dirs (); + test_get_cache_home (); + test_get_user_log_dir (); return 0; } === modified file 'init/xdg.c' --- init/xdg.c 2012-12-18 16:31:55 +0000 +++ init/xdg.c 2013-01-22 12:01:56 +0000 @@ -24,6 +24,8 @@ #endif /* HAVE_CONFIG_H */ #include <stdlib.h> +#include <sys/stat.h> +#include <sys/types.h> #include <nih/alloc.h> #include <nih/logging.h> @@ -33,22 +35,37 @@ #include "xdg.h" /** + * user_mode: + * + * If TRUE, upstart runs in user session mode. + **/ +int user_mode = FALSE; + +/** * get_home_subdir: - * - * Construct path to directory in user's HOME dir. + * @suffix: sub-directory name + * @create: flag to create sub-directory + * + * Construct path to @suffix directory in user's HOME 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_home_subdir (const char * suffix) +get_home_subdir (const char * suffix, int create) { char *dir; - nih_assert (suffix && suffix[0]); + nih_assert (suffix != NULL); + nih_assert (suffix[0]); dir = getenv ("HOME"); - if (dir && dir[0]) { + if (dir && dir[0] == '/') { dir = nih_sprintf (NULL, "%s/%s", dir, suffix); + if (! dir) + return NULL; + if (create) + mkdir (dir, 0700); return dir; } @@ -56,6 +73,39 @@ } /** + * 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) +{ + nih_local char **env = NULL; + char *dir; + + dir = getenv ("XDG_CACHE_HOME"); + + if (dir && dir[0] == '/') { + mkdir (dir, 0700); + 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 @@ -70,12 +120,17 @@ dir = getenv ("XDG_CONFIG_HOME"); - if (dir && dir[0]) { + if (dir && dir[0] == '/') { + mkdir (dir, 0700); dir = nih_strdup (NULL, dir); return dir; } - dir = get_home_subdir (".config"); + /* 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; } @@ -134,6 +189,7 @@ if (path && path[0]) { if (! nih_strcat_sprintf (&path, NULL, "/%s", INIT_XDG_SUBDIR)) goto error; + mkdir (path, 0700); if (! nih_str_array_add (&all_dirs, NULL, NULL, path)) goto error; nih_free (path); @@ -141,7 +197,7 @@ } /* Legacy User's: ~/.init */ - path = get_home_subdir (USERCONFDIR); + path = get_home_subdir (USERCONFDIR, FALSE); if (! path) goto error; @@ -158,6 +214,8 @@ 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)) @@ -185,3 +243,26 @@ 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, 0700); + return dir; + } + return NULL; +} === modified file 'init/xdg.h' --- init/xdg.h 2012-12-18 13:58:23 +0000 +++ init/xdg.h 2013-01-23 12:56:00 +0000 @@ -25,18 +25,24 @@ NIH_BEGIN_EXTERN -char * get_home_subdir (const char * suffix) +char * get_home_subdir (const char * suffix, int create) __attribute__ ((malloc, warn_unused_result)); char * xdg_get_config_home (void) __attribute__ ((malloc, warn_unused_result)); +char * xdg_get_cache_home (void) + __attribute__ ((malloc, warn_unused_result)); + char ** xdg_get_config_dirs (void) __attribute__ ((malloc, warn_unused_result)); char ** get_user_upstart_dirs (void) __attribute__ ((malloc, warn_unused_result)); +char * get_user_log_dir (void) + __attribute__ ((malloc, warn_unused_result)); + NIH_END_EXTERN #endif /* INIT_XDG_H */
-- upstart-devel mailing list [email protected] Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/upstart-devel
