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