PID is now also tried to be stored to: /tmp
[udpgate.git] / src / configuration-pathname.c
1 /* $Id$
2  * UDP Gateway persistent configuration pathname detection
3  * Copyright (C) 2005 Jan Kratochvil <project-udpgate@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
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.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <glib/gmem.h>
24 #include <glib/gstrfuncs.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <errno.h>
28
29 #include "configuration-pathname.h"     /* self */
30
31
32 /* OK, GConf would be nice.
33  * Unfortunately the fully-static build does not support GConf process spawn at
34  * all. Also /etc/sysconfig directory is standard for the daemon services.
35  */
36
37
38 static const gchar *static_pathname;
39 static gboolean verbose=FALSE;
40
41
42 static gboolean try_pairname(const gchar *dirname,const gchar *filename)
43 {
44 gchar *pathname;
45
46         g_return_val_if_fail(dirname!=NULL,FALSE);
47         g_return_val_if_fail(filename!=NULL,FALSE);
48
49         if (static_pathname)
50                 return FALSE;
51         if (access(dirname,W_OK|X_OK)) {
52                 if (verbose)
53                         g_warning(_("Configuration directory not accessible(X) and/or writable(W): %s"),dirname);
54                 return FALSE;
55                 }
56         pathname=g_strdup_printf("%s/%s",dirname,filename);
57         if (access(pathname,R_OK|W_OK) && errno!=ENOENT) {
58                 if (verbose)
59                         g_warning(_("Configuration pathname not readable(R) and/or writable(W): %s"),pathname);
60                 g_free(pathname);
61                 return FALSE;
62                 }
63         static_pathname=pathname;
64         return TRUE;
65 }
66
67 G_CONST_RETURN gchar *configuration_pathname(void)
68 {
69 const gchar *home_val;
70 static gboolean first=TRUE;
71
72         if (!static_pathname && first) {
73                 try_pairname(G_STRINGIFY(SYSCONFDIR) "/sysconfig",PACKAGE);
74                 try_pairname(G_STRINGIFY(SYSCONFDIR) "/default",PACKAGE);
75                 try_pairname(G_STRINGIFY(SYSCONFDIR) "",PACKAGE);
76                 if ((home_val=getenv("HOME")))
77                         try_pairname(home_val,"." PACKAGE "rc");
78                 if (!static_pathname && !verbose) {
79                         verbose=TRUE;
80                         configuration_pathname();
81                         g_warning(_("All automatic configuration pathnames failed; configuration persistency disabled."));
82                         }
83                 first=FALSE;
84                 }
85         return static_pathname;
86 }