cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

known_dirs.c
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #include <known_dirs.h>
26 #include <definitions.h>
27 #include <file_lib.h>
28 
29 static char OVERRIDE_BINDIR[PATH_MAX] = {0};
30 
31 #if defined(__CYGWIN__) || defined(__ANDROID__)
32 
33 #define GET_DEFAULT_DIRECTORY_DEFINE(FUNC, GLOBAL) \
34 static const char *GetDefault##FUNC##Dir(void) \
35 { \
36  return GLOBAL; \
37 }
38 
39 /* getpwuid() on Android returns /data,
40  * so use compile-time default instead */
41 GET_DEFAULT_DIRECTORY_DEFINE(Work, WORKDIR)
45 GET_DEFAULT_DIRECTORY_DEFINE(Input, INPUTDIR)
46 GET_DEFAULT_DIRECTORY_DEFINE(Master, MASTERDIR)
47 GET_DEFAULT_DIRECTORY_DEFINE(State, STATEDIR)
48 
49 #elif !defined(__MINGW32__)
50 
51 const char *GetDefaultDir_helper(char *dir, const char *root_dir,
52  const char *append_dir)
53 {
54  assert(dir != NULL);
55 
56  if (getuid() > 0)
57  {
58  if (dir[0] == '\0')
59  {
60  struct passwd *mpw = getpwuid(getuid());
61 
62  if (mpw == NULL)
63  {
64  return NULL;
65  }
66 
67  if ( append_dir == NULL )
68  {
69  if (snprintf(dir, PATH_MAX, "%s/.cfagent",
70  mpw->pw_dir) >= PATH_MAX)
71  {
72  return NULL;
73  }
74  }
75  else
76  {
77  if (snprintf(dir, PATH_MAX, "%s/.cfagent/%s",
78  mpw->pw_dir, append_dir) >= PATH_MAX)
79  {
80  return NULL;
81  }
82  }
83  }
84  return dir;
85  }
86  else
87  {
88  return root_dir;
89  }
90 }
91 
92 #endif
93 
94 #define GET_DEFAULT_DIRECTORY_DEFINE(FUNC, STATIC, GLOBAL, FOLDER) \
95 const char *GetDefault##FUNC##Dir(void) \
96 { \
97  static char STATIC##dir[PATH_MAX]; /* GLOBAL_C */ \
98  return GetDefaultDir_helper(STATIC##dir, GLOBAL, FOLDER); \
99 }
100 
101 #if !defined(__CYGWIN__) && !defined(__ANDROID__)
102 GET_DEFAULT_DIRECTORY_DEFINE(Work, work, WORKDIR, NULL)
103 GET_DEFAULT_DIRECTORY_DEFINE(Bin, bin, BINDIR, "bin")
104 GET_DEFAULT_DIRECTORY_DEFINE(Log, log, LOGDIR, "log")
106 GET_DEFAULT_DIRECTORY_DEFINE(Master, master, MASTERDIR, "masterfiles")
107 GET_DEFAULT_DIRECTORY_DEFINE(Input, input, INPUTDIR, "inputs")
109 #endif
110 
111 
112 /*******************************************************************/
113 
114 const char *GetWorkDir(void)
115 {
116  const char *workdir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR");
117 
118  return workdir == NULL ? GetDefaultWorkDir() : workdir;
119 }
120 
121 const char *GetBinDir(void)
122 {
123  const char *workdir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR");
124 
125  if (workdir == NULL)
126  {
127 #ifdef __MINGW32__
128  /* Compile-time default bindir doesn't work on windows because during
129  * the build /var/cfengine/bin is used and only when the package is
130  * created, things are shuffled around */
131  snprintf(OVERRIDE_BINDIR, PATH_MAX, "%s%cbin",
133  return OVERRIDE_BINDIR;
134 #else
135  return GetDefaultBinDir();
136 #endif
137  }
138  else
139  {
140  snprintf(OVERRIDE_BINDIR, PATH_MAX, "%s%cbin",
141  workdir, FILE_SEPARATOR);
142  return OVERRIDE_BINDIR;
143  }
144 }
145 
146 const char *GetLogDir(void)
147 {
148  const char *logdir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR");
149 
150  return logdir == NULL ? GetDefaultLogDir() : logdir;
151 }
152 
153 const char *GetPidDir(void)
154 {
155  const char *piddir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR");
156 
157  return piddir == NULL ? GetDefaultPidDir() : piddir;
158 }
159 
160 #define GET_DIRECTORY_DEFINE_FUNC_BODY(FUNC, VAR, GLOBAL, FOLDER) \
161 { \
162  const char *VAR##dir = getenv("CFENGINE_TEST_OVERRIDE_WORKDIR"); \
163  \
164  static char workbuf[CF_BUFSIZE]; \
165  \
166  if (VAR##dir != NULL) \
167  { \
168  snprintf(workbuf, CF_BUFSIZE, "%s/" #FOLDER, VAR##dir); \
169  } \
170  else if (strcmp(GLOBAL##DIR, "default") == 0 ) \
171  { \
172  snprintf(workbuf, CF_BUFSIZE, "%s/" #FOLDER, GetWorkDir()); \
173  } \
174  else /* VAR##dir defined at compile-time */ \
175  { \
176  return GetDefault##FUNC##Dir(); \
177  } \
178  \
179  return MapName(workbuf); \
180 }
181 
182 const char *GetInputDir(void)
183  GET_DIRECTORY_DEFINE_FUNC_BODY(Input, input, INPUT, inputs)
184 const char *GetMasterDir(void)
185  GET_DIRECTORY_DEFINE_FUNC_BODY(Master, master, MASTER, masterfiles)
186 const char *GetStateDir(void)
static int input(void)
Definition: cf3lex.c:2154
#define FILE_SEPARATOR
Definition: file_lib.h:102
#define NULL
Definition: getopt1.c:56
#define const
Definition: getopt.c:38
char * getenv(char *name)
const char * GetDefaultDir_helper(char *dir, const char *root_dir, const char *append_dir)
Definition: known_dirs.c:51
const char * GetInputDir(void)
Definition: known_dirs.c:182
const char * GetLogDir(void)
Definition: known_dirs.c:146
const char * GetDefaultLogDir(void)
Definition: known_dirs.c:104
#define GET_DEFAULT_DIRECTORY_DEFINE(FUNC, STATIC, GLOBAL, FOLDER)
Definition: known_dirs.c:94
const char * GetPidDir(void)
Definition: known_dirs.c:153
const char * GetStateDir(void)
Definition: known_dirs.c:186
const char * GetBinDir(void)
Definition: known_dirs.c:121
const char * GetMasterDir(void)
Definition: known_dirs.c:184
const char * GetDefaultPidDir(void)
Definition: known_dirs.c:105
const char * GetWorkDir(void)
Definition: known_dirs.c:114
const char * GetDefaultWorkDir(void)
Definition: known_dirs.c:102
static char OVERRIDE_BINDIR[4096]
Definition: known_dirs.c:29
const char * GetDefaultBinDir(void)
Definition: known_dirs.c:103
#define GET_DIRECTORY_DEFINE_FUNC_BODY(FUNC, VAR, GLOBAL, FOLDER)
Definition: known_dirs.c:160
void Log(LogLevel level, const char *fmt,...)
Definition: logging.c:409
uid_t getuid(void)
#define PATH_MAX
Definition: platform.h:176
state
Definition: rlist.c:708