]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
4 * Environment management routines for the CUPS scheduler.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
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/".
17 * Include necessary headers...
27 static int num_common_env
= 0; /* Number of common env vars */
28 static char *common_env
[MAX_ENV
]; /* Common env vars */
35 static void clear_env(void);
36 static int find_env(const char *name
);
40 * 'cupsdInitEnv()' - Initialize the current environment with standard variables.
47 * Clear existing environment variables...
52 #if defined(__APPLE__)
54 * Add special voodoo magic for MacOS X - this allows MacOS X
55 * programs to access their bundle resources properly...
57 * This string is replaced in cupsdStartProcess()...
60 cupsdSetString(common_env
, "<CFProcessPath>");
62 #endif /* __APPLE__ */
67 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
70 int /* O - Number of environment variables */
71 cupsdLoadEnv(char *envp
[], /* I - Environment array */
72 int envmax
) /* I - Maximum number of elements */
74 int i
; /* Looping var */
78 * Leave room for a NULL pointer at the end...
84 * Copy pointers to the environment...
87 for (i
= 0; i
< num_common_env
&& i
< envmax
; i
++)
88 envp
[i
] = common_env
[i
];
91 * NULL terminate the environment array and return the number of
92 * elements we added...
102 * 'cupsdSetEnv()' - Set a common environment variable.
106 cupsdSetEnv(const char *name
, /* I - Name of variable */
107 const char *value
) /* I - Value of variable */
109 int i
; /* Index into environent array */
113 * If "value" is NULL, try getting value from current environment...
117 value
= getenv(name
);
123 * Do not allow dynamic linker variables when running as root...
126 if (!RunUser
&& (!strncmp(name
, "DYLD_", 5) || !strncmp(name
, "LD_", 3)))
130 * See if this variable has already been defined...
133 if ((i
= find_env(name
)) < 0)
139 if (num_common_env
>= (int)(sizeof(common_env
) / sizeof(common_env
[0])))
141 cupsdLogMessage(CUPSD_LOG_ERROR
,
142 "cupsdSetEnv: Too many environment variables set!");
151 * Set the new environment variable...
154 cupsdSetStringf(common_env
+ i
, "%s=%s", name
, value
);
156 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetEnv: %s", common_env
[i
]);
161 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
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 */
169 char v
[4096]; /* Formatting string value */
170 va_list ap
; /* Argument pointer */
174 * Format the value string...
178 vsnprintf(v
, sizeof(v
), value
, ap
);
182 * Set the env variable...
185 cupsdSetEnv(name
, v
);
190 * 'cupsdUpdateEnv()' - Update the environment for the configured directories.
197 * Set common variables...
200 #define set_if_undefined(name,value) if (find_env(name) < 0) cupsdSetEnv(name,value)
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
);
227 cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE
- 1);
232 * 'clear_env()' - Clear common environment variables.
238 int i
; /* Looping var */
241 for (i
= 0; i
< num_common_env
; i
++)
242 cupsdClearString(common_env
+ i
);
249 * 'find_env()' - Find a common environment variable.
252 static int /* O - Index or -1 if not found */
253 find_env(const char *name
) /* I - Variable name */
255 int i
; /* Looping var */
256 size_t namelen
; /* Length of name */
259 for (i
= 0, namelen
= strlen(name
); i
< num_common_env
; i
++)
260 if (!strncmp(common_env
[i
], name
, namelen
) && common_env
[i
][namelen
] == '=')