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