]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
10d09e33 | 4 | * Environment management routines for the CUPS scheduler. |
ef416fc2 | 5 | * |
0268488e | 6 | * Copyright 2007-2011 by Apple Inc. |
e00b005a | 7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 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/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
0268488e MS |
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. | |
ef416fc2 | 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 */ | |
e00b005a | 39 | static char *common_env[MAX_ENV]; /* Common env vars */ |
ef416fc2 | 40 | |
41 | ||
42 | /* | |
e1d6a774 | 43 | * Local functions... |
ef416fc2 | 44 | */ |
45 | ||
e1d6a774 | 46 | static void clear_env(void); |
0268488e | 47 | static int find_env(const char *name); |
ef416fc2 | 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 | ||
e1d6a774 | 61 | clear_env(); |
ef416fc2 | 62 | |
bd7854cb | 63 | #if defined(__APPLE__) |
ef416fc2 | 64 | /* |
a29fd7dd | 65 | * Add special voodoo magic for MacOS X - this allows MacOS X |
e53920b9 | 66 | * programs to access their bundle resources properly... |
bd7854cb | 67 | * |
e53920b9 | 68 | * This string is replaced in cupsdStartProcess()... |
ef416fc2 | 69 | */ |
70 | ||
71 | cupsdSetString(common_env, "<CFProcessPath>"); | |
72 | num_common_env = 1; | |
bd7854cb | 73 | #endif /* __APPLE__ */ |
ef416fc2 | 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 | { | |
0268488e | 120 | int i; /* Index into environent array */ |
ef416fc2 | 121 | |
ef416fc2 | 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 | /* | |
480ef0fe | 134 | * See if this variable has already been defined... |
ef416fc2 | 135 | */ |
136 | ||
0268488e | 137 | if ((i = find_env(name)) < 0) |
480ef0fe | 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 | ||
0268488e | 150 | i = num_common_env; |
480ef0fe | 151 | num_common_env ++; |
152 | } | |
153 | ||
154 | /* | |
155 | * Set the new environment variable... | |
156 | */ | |
ef416fc2 | 157 | |
480ef0fe | 158 | cupsdSetStringf(common_env + i, "%s=%s", name, value); |
ef416fc2 | 159 | |
75bd9771 | 160 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]); |
ef416fc2 | 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 | ||
0268488e MS |
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); | |
a29fd7dd MS |
230 | |
231 | cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE - 1); | |
0268488e MS |
232 | } |
233 | ||
234 | ||
ef416fc2 | 235 | /* |
e1d6a774 | 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 | /* | |
515b46cd MS |
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 | /* | |
f2d18633 | 272 | * End of "$Id$". |
ef416fc2 | 273 | */ |