]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
5e6c3df7 | 4 | * Environment management routines for the CUPS scheduler. |
ef416fc2 | 5 | * |
5e6c3df7 MS |
6 | * Copyright 2007-2014 by Apple Inc. |
7 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
ef416fc2 | 8 | * |
5e6c3df7 MS |
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/". | |
ef416fc2 | 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 */ | |
e00b005a | 28 | static char *common_env[MAX_ENV]; /* Common env vars */ |
ef416fc2 | 29 | |
30 | ||
31 | /* | |
e1d6a774 | 32 | * Local functions... |
ef416fc2 | 33 | */ |
34 | ||
e1d6a774 | 35 | static void clear_env(void); |
0268488e | 36 | static int find_env(const char *name); |
ef416fc2 | 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 | ||
e1d6a774 | 50 | clear_env(); |
ef416fc2 | 51 | |
bd7854cb | 52 | #if defined(__APPLE__) |
ef416fc2 | 53 | /* |
a29fd7dd | 54 | * Add special voodoo magic for MacOS X - this allows MacOS X |
e53920b9 | 55 | * programs to access their bundle resources properly... |
bd7854cb | 56 | * |
e53920b9 | 57 | * This string is replaced in cupsdStartProcess()... |
ef416fc2 | 58 | */ |
59 | ||
60 | cupsdSetString(common_env, "<CFProcessPath>"); | |
61 | num_common_env = 1; | |
bd7854cb | 62 | #endif /* __APPLE__ */ |
ef416fc2 | 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 | { | |
0268488e | 109 | int i; /* Index into environent array */ |
ef416fc2 | 110 | |
ef416fc2 | 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 | ||
5e6c3df7 MS |
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 | ||
ef416fc2 | 129 | /* |
480ef0fe | 130 | * See if this variable has already been defined... |
ef416fc2 | 131 | */ |
132 | ||
0268488e | 133 | if ((i = find_env(name)) < 0) |
480ef0fe | 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 | ||
0268488e | 146 | i = num_common_env; |
480ef0fe | 147 | num_common_env ++; |
148 | } | |
149 | ||
150 | /* | |
151 | * Set the new environment variable... | |
152 | */ | |
ef416fc2 | 153 | |
480ef0fe | 154 | cupsdSetStringf(common_env + i, "%s=%s", name, value); |
ef416fc2 | 155 | |
75bd9771 | 156 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSetEnv: %s", common_env[i]); |
ef416fc2 | 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 | ||
0268488e MS |
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); | |
a29fd7dd MS |
226 | |
227 | cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE - 1); | |
0268488e MS |
228 | } |
229 | ||
230 | ||
ef416fc2 | 231 | /* |
e1d6a774 | 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 | /* | |
515b46cd MS |
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 | /* | |
f2d18633 | 268 | * End of "$Id$". |
ef416fc2 | 269 | */ |