]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
2 * Environment management routines for the CUPS scheduler.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
15 * Include necessary headers...
25 static int num_common_env
= 0; /* Number of common env vars */
26 static char *common_env
[MAX_ENV
]; /* Common env vars */
33 static void clear_env(void);
34 static int find_env(const char *name
);
38 * 'cupsdInitEnv()' - Initialize the current environment with standard variables.
45 * Clear existing environment variables...
50 #if defined(__APPLE__)
52 * Add special voodoo magic for MacOS X - this allows MacOS X
53 * programs to access their bundle resources properly...
55 * This string is replaced in cupsdStartProcess()...
58 cupsdSetString(common_env
, "<CFProcessPath>");
60 #endif /* __APPLE__ */
65 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
68 int /* O - Number of environment variables */
69 cupsdLoadEnv(char *envp
[], /* I - Environment array */
70 int envmax
) /* I - Maximum number of elements */
72 int i
; /* Looping var */
76 * Leave room for a NULL pointer at the end...
82 * Copy pointers to the environment...
85 for (i
= 0; i
< num_common_env
&& i
< envmax
; i
++)
86 envp
[i
] = common_env
[i
];
89 * NULL terminate the environment array and return the number of
90 * elements we added...
100 * 'cupsdSetEnv()' - Set a common environment variable.
104 cupsdSetEnv(const char *name
, /* I - Name of variable */
105 const char *value
) /* I - Value of variable */
107 int i
; /* Index into environent array */
111 * If "value" is NULL, try getting value from current environment...
115 value
= getenv(name
);
121 * Do not allow dynamic linker variables when running as root...
124 if (!RunUser
&& (!strncmp(name
, "DYLD_", 5) || !strncmp(name
, "LD_", 3)))
128 * See if this variable has already been defined...
131 if ((i
= find_env(name
)) < 0)
137 if (num_common_env
>= (int)(sizeof(common_env
) / sizeof(common_env
[0])))
139 cupsdLogMessage(CUPSD_LOG_ERROR
,
140 "cupsdSetEnv: Too many environment variables set!");
149 * Set the new environment variable...
152 cupsdSetStringf(common_env
+ i
, "%s=%s", name
, value
);
154 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetEnv: %s", common_env
[i
]);
159 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
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 */
167 char v
[4096]; /* Formatting string value */
168 va_list ap
; /* Argument pointer */
172 * Format the value string...
176 vsnprintf(v
, sizeof(v
), value
, ap
);
180 * Set the env variable...
183 cupsdSetEnv(name
, v
);
188 * 'cupsdUpdateEnv()' - Update the environment for the configured directories.
195 * Set common variables...
198 #define set_if_undefined(name,value) if (find_env(name) < 0) cupsdSetEnv(name,value)
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
);
225 cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE
- 1);
230 * 'clear_env()' - Clear common environment variables.
236 int i
; /* Looping var */
239 for (i
= 0; i
< num_common_env
; i
++)
240 cupsdClearString(common_env
+ i
);
247 * 'find_env()' - Find a common environment variable.
250 static int /* O - Index or -1 if not found */
251 find_env(const char *name
) /* I - Variable name */
253 int i
; /* Looping var */
254 size_t namelen
; /* Length of name */
257 for (i
= 0, namelen
= strlen(name
); i
< num_common_env
; i
++)
258 if (!strncmp(common_env
[i
], name
, namelen
) && common_env
[i
][namelen
] == '=')