]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
a2d0ffcebea4f60aa4e960a1ee0cb14890e50327
[thirdparty/cups.git] / scheduler / env.c
1 /*
2 * Environment management routines for the CUPS scheduler.
3 *
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
6 *
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
12 */
13
14 /*
15 * Include necessary headers...
16 */
17
18 #include "cupsd.h"
19
20
21 /*
22 * Local globals...
23 */
24
25 static int num_common_env = 0; /* Number of common env vars */
26 static char *common_env[MAX_ENV]; /* Common env vars */
27
28
29 /*
30 * Local functions...
31 */
32
33 static void clear_env(void);
34 static int find_env(const char *name);
35
36
37 /*
38 * 'cupsdInitEnv()' - Initialize the current environment with standard variables.
39 */
40
41 void
42 cupsdInitEnv(void)
43 {
44 /*
45 * Clear existing environment variables...
46 */
47
48 clear_env();
49
50 #if defined(__APPLE__)
51 /*
52 * Add special voodoo magic for macOS - this allows macOS
53 * programs to access their bundle resources properly...
54 *
55 * This string is replaced in cupsdStartProcess()...
56 */
57
58 cupsdSetString(common_env, "<CFProcessPath>");
59 num_common_env = 1;
60 #endif /* __APPLE__ */
61 }
62
63
64 /*
65 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
66 */
67
68 int /* O - Number of environment variables */
69 cupsdLoadEnv(char *envp[], /* I - Environment array */
70 int envmax) /* I - Maximum number of elements */
71 {
72 int i; /* Looping var */
73
74
75 /*
76 * Leave room for a NULL pointer at the end...
77 */
78
79 envmax --;
80
81 /*
82 * Copy pointers to the environment...
83 */
84
85 for (i = 0; i < num_common_env && i < envmax; i ++)
86 envp[i] = common_env[i];
87
88 /*
89 * NULL terminate the environment array and return the number of
90 * elements we added...
91 */
92
93 envp[i] = NULL;
94
95 return (i);
96 }
97
98
99 /*
100 * 'cupsdSetEnv()' - Set a common environment variable.
101 */
102
103 void
104 cupsdSetEnv(const char *name, /* I - Name of variable */
105 const char *value) /* I - Value of variable */
106 {
107 int i; /* Index into environent array */
108
109
110 /*
111 * If "value" is NULL, try getting value from current environment...
112 */
113
114 if (!value)
115 value = getenv(name);
116
117 if (!value)
118 return;
119
120 /*
121 * Do not allow dynamic linker variables when running as root...
122 */
123
124 if (!RunUser && (!strncmp(name, "DYLD_", 5) || !strncmp(name, "LD_", 3)))
125 return;
126
127 /*
128 * See if this variable has already been defined...
129 */
130
131 if ((i = find_env(name)) < 0)
132 {
133 /*
134 * Check for room...
135 */
136
137 if (num_common_env >= (int)(sizeof(common_env) / sizeof(common_env[0])))
138 {
139 cupsdLogMessage(CUPSD_LOG_ERROR,
140 "cupsdSetEnv: Too many environment variables set!");
141 return;
142 }
143
144 i = num_common_env;
145 num_common_env ++;
146 }
147
148 /*
149 * Set the new environment variable...
150 */
151
152 cupsdSetStringf(common_env + i, "%s=%s", name, value);
153
154 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]);
155 }
156
157
158 /*
159 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
160 */
161
162 void
163 cupsdSetEnvf(const char *name, /* I - Name of variable */
164 const char *value, /* I - Printf-style value of variable */
165 ...) /* I - Additional args as needed */
166 {
167 char v[4096]; /* Formatting string value */
168 va_list ap; /* Argument pointer */
169
170
171 /*
172 * Format the value string...
173 */
174
175 va_start(ap, value);
176 vsnprintf(v, sizeof(v), value, ap);
177 va_end(ap);
178
179 /*
180 * Set the env variable...
181 */
182
183 cupsdSetEnv(name, v);
184 }
185
186
187 /*
188 * 'cupsdUpdateEnv()' - Update the environment for the configured directories.
189 */
190
191 void
192 cupsdUpdateEnv(void)
193 {
194 /*
195 * Set common variables...
196 */
197
198 #define set_if_undefined(name,value) if (find_env(name) < 0) cupsdSetEnv(name,value)
199
200 set_if_undefined("CUPS_CACHEDIR", CacheDir);
201 set_if_undefined("CUPS_DATADIR", DataDir);
202 set_if_undefined("CUPS_DOCROOT", DocumentRoot);
203 set_if_undefined("CUPS_FONTPATH", FontPath);
204 set_if_undefined("CUPS_REQUESTROOT", RequestRoot);
205 set_if_undefined("CUPS_SERVERBIN", ServerBin);
206 set_if_undefined("CUPS_SERVERROOT", ServerRoot);
207 set_if_undefined("CUPS_STATEDIR", StateDir);
208 set_if_undefined("DYLD_LIBRARY_PATH", NULL);
209 set_if_undefined("HOME", TempDir);
210 set_if_undefined("LD_ASSUME_KERNEL", NULL);
211 set_if_undefined("LD_LIBRARY_PATH", NULL);
212 set_if_undefined("LD_PRELOAD", NULL);
213 set_if_undefined("NLSPATH", NULL);
214 if (find_env("PATH") < 0)
215 cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR ":" CUPS_SBINDIR
216 ":/bin:/usr/bin", ServerBin);
217 set_if_undefined("SERVER_ADMIN", ServerAdmin);
218 set_if_undefined("SHLIB_PATH", NULL);
219 set_if_undefined("SOFTWARE", CUPS_MINIMAL);
220 set_if_undefined("TMPDIR", TempDir);
221 set_if_undefined("TZ", NULL);
222 set_if_undefined("USER", "root");
223 set_if_undefined("VG_ARGS", NULL);
224
225 cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE - 1);
226 }
227
228
229 /*
230 * 'clear_env()' - Clear common environment variables.
231 */
232
233 static void
234 clear_env(void)
235 {
236 int i; /* Looping var */
237
238
239 for (i = 0; i < num_common_env; i ++)
240 cupsdClearString(common_env + i);
241
242 num_common_env = 0;
243 }
244
245
246 /*
247 * 'find_env()' - Find a common environment variable.
248 */
249
250 static int /* O - Index or -1 if not found */
251 find_env(const char *name) /* I - Variable name */
252 {
253 int i; /* Looping var */
254 size_t namelen; /* Length of name */
255
256
257 for (i = 0, namelen = strlen(name); i < num_common_env; i ++)
258 if (!strncmp(common_env[i], name, namelen) && common_env[i][namelen] == '=')
259 return (i);
260
261 return (-1);
262 }