]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/env.c
2 * Environment management routines for the CUPS scheduler.
4 * Copyright 2007-2016 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
21 static int num_common_env
= 0; /* Number of common env vars */
22 static char *common_env
[MAX_ENV
]; /* Common env vars */
29 static void clear_env(void);
30 static int find_env(const char *name
);
34 * 'cupsdInitEnv()' - Initialize the current environment with standard variables.
41 * Clear existing environment variables...
46 #if defined(__APPLE__)
48 * Add special voodoo magic for macOS - this allows macOS
49 * programs to access their bundle resources properly...
51 * This string is replaced in cupsdStartProcess()...
54 cupsdSetString(common_env
, "<CFProcessPath>");
56 #endif /* __APPLE__ */
61 * 'cupsdLoadEnv()' - Copy common environment variables into an array.
64 int /* O - Number of environment variables */
65 cupsdLoadEnv(char *envp
[], /* I - Environment array */
66 int envmax
) /* I - Maximum number of elements */
68 int i
; /* Looping var */
72 * Leave room for a NULL pointer at the end...
78 * Copy pointers to the environment...
81 for (i
= 0; i
< num_common_env
&& i
< envmax
; i
++)
82 envp
[i
] = common_env
[i
];
85 * NULL terminate the environment array and return the number of
86 * elements we added...
96 * 'cupsdSetEnv()' - Set a common environment variable.
100 cupsdSetEnv(const char *name
, /* I - Name of variable */
101 const char *value
) /* I - Value of variable */
103 int i
; /* Index into environent array */
107 * If "value" is NULL, try getting value from current environment...
111 value
= getenv(name
);
117 * Do not allow dynamic linker variables when running as root...
120 if (!RunUser
&& (!strncmp(name
, "DYLD_", 5) || !strncmp(name
, "LD_", 3)))
124 * See if this variable has already been defined...
127 if ((i
= find_env(name
)) < 0)
133 if (num_common_env
>= (int)(sizeof(common_env
) / sizeof(common_env
[0])))
135 cupsdLogMessage(CUPSD_LOG_ERROR
,
136 "cupsdSetEnv: Too many environment variables set!");
145 * Set the new environment variable...
148 cupsdSetStringf(common_env
+ i
, "%s=%s", name
, value
);
150 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetEnv: %s", common_env
[i
]);
155 * 'cupsdSetEnvf()' - Set a formatted common environment variable.
159 cupsdSetEnvf(const char *name
, /* I - Name of variable */
160 const char *value
, /* I - Printf-style value of variable */
161 ...) /* I - Additional args as needed */
163 char v
[4096]; /* Formatting string value */
164 va_list ap
; /* Argument pointer */
168 * Format the value string...
172 vsnprintf(v
, sizeof(v
), value
, ap
);
176 * Set the env variable...
179 cupsdSetEnv(name
, v
);
184 * 'cupsdUpdateEnv()' - Update the environment for the configured directories.
191 * Set common variables...
194 #define set_if_undefined(name,value) if (find_env(name) < 0) cupsdSetEnv(name,value)
196 set_if_undefined("CUPS_CACHEDIR", CacheDir
);
197 set_if_undefined("CUPS_DATADIR", DataDir
);
198 set_if_undefined("CUPS_DOCROOT", DocumentRoot
);
199 set_if_undefined("CUPS_FONTPATH", FontPath
);
200 set_if_undefined("CUPS_REQUESTROOT", RequestRoot
);
201 set_if_undefined("CUPS_SERVERBIN", ServerBin
);
202 set_if_undefined("CUPS_SERVERROOT", ServerRoot
);
203 set_if_undefined("CUPS_STATEDIR", StateDir
);
204 set_if_undefined("DYLD_INSERT_LIBRARIES", NULL
);
205 set_if_undefined("DYLD_LIBRARY_PATH", NULL
);
206 set_if_undefined("HOME", TempDir
);
207 set_if_undefined("LD_ASSUME_KERNEL", NULL
);
208 set_if_undefined("LD_LIBRARY_PATH", NULL
);
209 set_if_undefined("LD_PRELOAD", NULL
);
210 set_if_undefined("NLSPATH", NULL
);
211 if (find_env("PATH") < 0)
212 cupsdSetEnvf("PATH", "%s/filter:" CUPS_BINDIR
":" CUPS_SBINDIR
213 ":/bin:/usr/bin", ServerBin
);
214 set_if_undefined("SERVER_ADMIN", ServerAdmin
);
215 set_if_undefined("SHLIB_PATH", NULL
);
216 set_if_undefined("SOFTWARE", CUPS_MINIMAL
);
217 set_if_undefined("TMPDIR", TempDir
);
218 set_if_undefined("TZ", NULL
);
219 set_if_undefined("USER", "root");
220 set_if_undefined("VG_ARGS", NULL
);
222 cupsdSetEnvf("CUPS_MAX_MESSAGE", "%d", CUPSD_SB_BUFFER_SIZE
- 1);
227 * 'clear_env()' - Clear common environment variables.
233 int i
; /* Looping var */
236 for (i
= 0; i
< num_common_env
; i
++)
237 cupsdClearString(common_env
+ i
);
244 * 'find_env()' - Find a common environment variable.
247 static int /* O - Index or -1 if not found */
248 find_env(const char *name
) /* I - Variable name */
250 int i
; /* Looping var */
251 size_t namelen
; /* Length of name */
254 for (i
= 0, namelen
= strlen(name
); i
< num_common_env
; i
++)
255 if (!strncmp(common_env
[i
], name
, namelen
) && common_env
[i
][namelen
] == '=')