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