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