]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/globals.c
Greatly simplify the man page handling.
[thirdparty/cups.git] / cups / globals.c
CommitLineData
ef416fc2 1/*
f787e1e3 2 * Global variable access routines for CUPS.
ef416fc2 3 *
f787e1e3
MS
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 6 *
e3101897 7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
ef416fc2 8 */
9
10/*
11 * Include necessary headers...
12 */
13
71e16022 14#include "cups-private.h"
ef416fc2 15
16
17/*
6d2f911b 18 * Local globals...
ef416fc2 19 */
20
a469f8a5
MS
21#ifdef DEBUG
22static int cups_global_index = 0;
23 /* Next thread number */
24#endif /* DEBUG */
6d2f911b
MS
25static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER;
26 /* Thread local storage key */
27#ifdef HAVE_PTHREAD_H
28static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT;
29 /* One-time initialization object */
30#endif /* HAVE_PTHREAD_H */
19dc16f7 31#if defined(HAVE_PTHREAD_H) || defined(_WIN32)
6d2f911b
MS
32static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER;
33 /* Global critical section */
19dc16f7 34#endif /* HAVE_PTHREAD_H || _WIN32 */
f8b3a85b 35
f8b3a85b 36
6d2f911b
MS
37/*
38 * Local functions...
39 */
f8b3a85b 40
19dc16f7 41#ifdef _WIN32
12f89d24 42static void cups_fix_path(char *path);
19dc16f7 43#endif /* _WIN32 */
6d2f911b 44static _cups_globals_t *cups_globals_alloc(void);
19dc16f7 45#if defined(HAVE_PTHREAD_H) || defined(_WIN32)
6d2f911b 46static void cups_globals_free(_cups_globals_t *g);
19dc16f7 47#endif /* HAVE_PTHREAD_H || _WIN32 */
6d2f911b
MS
48#ifdef HAVE_PTHREAD_H
49static void cups_globals_init(void);
50#endif /* HAVE_PTHREAD_H */
f8b3a85b 51
f8b3a85b 52
6d2f911b
MS
53/*
54 * '_cupsGlobalLock()' - Lock the global mutex.
55 */
f8b3a85b 56
6d2f911b
MS
57void
58_cupsGlobalLock(void)
59{
60#ifdef HAVE_PTHREAD_H
61 pthread_mutex_lock(&cups_global_mutex);
19dc16f7 62#elif defined(_WIN32)
cc754834 63 EnterCriticalSection(&cups_global_mutex.m_criticalSection);
6d2f911b
MS
64#endif /* HAVE_PTHREAD_H */
65}
f8b3a85b 66
f8b3a85b 67
6d2f911b
MS
68/*
69 * '_cupsGlobals()' - Return a pointer to thread local storage
70 */
f8b3a85b 71
6d2f911b
MS
72_cups_globals_t * /* O - Pointer to global data */
73_cupsGlobals(void)
74{
75 _cups_globals_t *cg; /* Pointer to global data */
f8b3a85b 76
f8b3a85b 77
6d2f911b
MS
78#ifdef HAVE_PTHREAD_H
79 /*
80 * Initialize the global data exactly once...
81 */
82
83 pthread_once(&cups_globals_key_once, cups_globals_init);
84#endif /* HAVE_PTHREAD_H */
85
86 /*
87 * See if we have allocated the data yet...
88 */
89
90 if ((cg = (_cups_globals_t *)_cupsThreadGetData(cups_globals_key)) == NULL)
5180a04c
MS
91 {
92 /*
6d2f911b 93 * No, allocate memory as set the pointer for the key...
5180a04c 94 */
ef416fc2 95
6d2f911b
MS
96 if ((cg = cups_globals_alloc()) != NULL)
97 _cupsThreadSetData(cups_globals_key, cg);
5180a04c 98 }
ef416fc2 99
6d2f911b
MS
100 /*
101 * Return the pointer to the data...
102 */
ef416fc2 103
6d2f911b
MS
104 return (cg);
105}
5180a04c 106
5180a04c 107
6d2f911b
MS
108/*
109 * '_cupsGlobalUnlock()' - Unlock the global mutex.
110 */
5180a04c 111
6d2f911b
MS
112void
113_cupsGlobalUnlock(void)
114{
115#ifdef HAVE_PTHREAD_H
116 pthread_mutex_unlock(&cups_global_mutex);
19dc16f7 117#elif defined(_WIN32)
cc754834 118 LeaveCriticalSection(&cups_global_mutex.m_criticalSection);
6d2f911b 119#endif /* HAVE_PTHREAD_H */
ef416fc2 120}
121
122
19dc16f7 123#ifdef _WIN32
ef416fc2 124/*
6d2f911b 125 * 'DllMain()' - Main entry for library.
ef416fc2 126 */
127
6d2f911b
MS
128BOOL WINAPI /* O - Success/failure */
129DllMain(HINSTANCE hinst, /* I - DLL module handle */
130 DWORD reason, /* I - Reason */
131 LPVOID reserved) /* I - Unused */
132{
133 _cups_globals_t *cg; /* Global data */
ef416fc2 134
ef416fc2 135
6d2f911b
MS
136 (void)hinst;
137 (void)reserved;
ef416fc2 138
6d2f911b
MS
139 switch (reason)
140 {
141 case DLL_PROCESS_ATTACH : /* Called on library initialization */
cc754834 142 InitializeCriticalSection(&cups_global_mutex.m_criticalSection);
6d2f911b
MS
143
144 if ((cups_globals_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
145 return (FALSE);
146 break;
147
148 case DLL_THREAD_DETACH : /* Called when a thread terminates */
149 if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL)
150 cups_globals_free(cg);
151 break;
152
153 case DLL_PROCESS_DETACH : /* Called when library is unloaded */
154 if ((cg = (_cups_globals_t *)TlsGetValue(cups_globals_key)) != NULL)
155 cups_globals_free(cg);
ef416fc2 156
6d2f911b 157 TlsFree(cups_globals_key);
cc754834 158 DeleteCriticalSection(&cups_global_mutex.m_criticalSection);
6d2f911b
MS
159 break;
160
161 default:
162 break;
163 }
164
165 return (TRUE);
166}
19dc16f7 167#endif /* _WIN32 */
ef416fc2 168
169
170/*
6d2f911b 171 * 'cups_globals_alloc()' - Allocate and initialize global data.
ef416fc2 172 */
173
6d2f911b
MS
174static _cups_globals_t * /* O - Pointer to global data */
175cups_globals_alloc(void)
ef416fc2 176{
6d2f911b
MS
177 _cups_globals_t *cg = malloc(sizeof(_cups_globals_t));
178 /* Pointer to global data */
19dc16f7 179#ifdef _WIN32
6d2f911b
MS
180 HKEY key; /* Registry key */
181 DWORD size; /* Size of string */
12f89d24
MS
182 static char installdir[1024] = "", /* Install directory */
183 confdir[1024] = "", /* Server root directory */
184 localedir[1024] = ""; /* Locale directory */
19dc16f7 185#endif /* _WIN32 */
6d2f911b 186
ef416fc2 187
6d2f911b
MS
188 if (!cg)
189 return (NULL);
ef416fc2 190
5180a04c 191 /*
6d2f911b
MS
192 * Clear the global storage and set the default encryption and password
193 * callback values...
ef416fc2 194 */
195
6d2f911b 196 memset(cg, 0, sizeof(_cups_globals_t));
c1420c87
MS
197 cg->encryption = (http_encryption_t)-1;
198 cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
08d56b1f 199 cg->trust_first = -1;
3abb875b
MS
200 cg->any_root = -1;
201 cg->expired_certs = -1;
202 cg->validate_certs = -1;
ef416fc2 203
a469f8a5
MS
204#ifdef DEBUG
205 /*
206 * Friendly thread ID for debugging...
207 */
208
209 cg->thread_id = ++ cups_global_index;
210#endif /* DEBUG */
211
ef416fc2 212 /*
6d2f911b
MS
213 * Then set directories as appropriate...
214 */
215
19dc16f7 216#ifdef _WIN32
12f89d24 217 if (!installdir[0])
ef416fc2 218 {
219 /*
12f89d24 220 * Open the registry...
ef416fc2 221 */
222
5a9febac 223 strlcpy(installdir, "C:/Program Files/cups.org", sizeof(installdir));
12f89d24 224
104c5283 225 if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, &key))
12f89d24
MS
226 {
227 /*
228 * Grab the installation directory...
229 */
230
231 char *ptr; /* Pointer into installdir */
232
233 size = sizeof(installdir);
104c5283 234 RegQueryValueExA(key, "installdir", NULL, NULL, installdir, &size);
12f89d24
MS
235 RegCloseKey(key);
236
237 for (ptr = installdir; *ptr;)
238 {
239 if (*ptr == '\\')
240 {
241 if (ptr[1])
242 *ptr++ = '/';
243 else
244 *ptr = '\0'; /* Strip trailing \ */
245 }
246 else if (*ptr == '/' && !ptr[1])
247 *ptr = '\0'; /* Strip trailing / */
248 else
249 ptr ++;
250 }
251 }
252
253 snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
254 snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
6d2f911b
MS
255 }
256
6d2f911b
MS
257 if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
258 cg->cups_datadir = installdir;
ef416fc2 259
6d2f911b
MS
260 if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
261 cg->cups_serverbin = installdir;
262
263 if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
264 cg->cups_serverroot = confdir;
265
266 if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
267 cg->cups_statedir = confdir;
268
269 if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
270 cg->localedir = localedir;
271
272#else
273# ifdef HAVE_GETEUID
274 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
275# else
276 if (!getuid())
277# endif /* HAVE_GETEUID */
278 {
ef416fc2 279 /*
6d2f911b
MS
280 * When running setuid/setgid, don't allow environment variables to override
281 * the directories...
ef416fc2 282 */
283
6d2f911b
MS
284 cg->cups_datadir = CUPS_DATADIR;
285 cg->cups_serverbin = CUPS_SERVERBIN;
286 cg->cups_serverroot = CUPS_SERVERROOT;
287 cg->cups_statedir = CUPS_STATEDIR;
288 cg->localedir = CUPS_LOCALEDIR;
ef416fc2 289 }
6d2f911b
MS
290 else
291 {
292 /*
293 * Allow directories to be overridden by environment variables.
294 */
ef416fc2 295
6d2f911b
MS
296 if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
297 cg->cups_datadir = CUPS_DATADIR;
ef416fc2 298
6d2f911b
MS
299 if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
300 cg->cups_serverbin = CUPS_SERVERBIN;
ef416fc2 301
6d2f911b
MS
302 if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
303 cg->cups_serverroot = CUPS_SERVERROOT;
ef416fc2 304
6d2f911b
MS
305 if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
306 cg->cups_statedir = CUPS_STATEDIR;
ef416fc2 307
6d2f911b
MS
308 if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
309 cg->localedir = CUPS_LOCALEDIR;
310 }
19dc16f7 311#endif /* _WIN32 */
6d2f911b
MS
312
313 return (cg);
ef416fc2 314}
315
316
317/*
6d2f911b 318 * 'cups_globals_free()' - Free global data.
ef416fc2 319 */
320
19dc16f7 321#if defined(HAVE_PTHREAD_H) || defined(_WIN32)
ef416fc2 322static void
6d2f911b 323cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */
ef416fc2 324{
dcb445bc 325 _cups_buffer_t *buffer, /* Current read/write buffer */
1f6f3dbc 326 *next; /* Next buffer */
fa73b229 327
328
e53920b9 329 if (cg->last_status_message)
5f64df29 330 _cupsStrFree(cg->last_status_message);
e53920b9 331
dcb445bc 332 for (buffer = cg->cups_buffers; buffer; buffer = next)
1f6f3dbc
MS
333 {
334 next = buffer->next;
335 free(buffer);
336 }
337
c168a833 338 cupsArrayDelete(cg->leg_size_lut);
f14324a7
MS
339 cupsArrayDelete(cg->ppd_size_lut);
340 cupsArrayDelete(cg->pwg_size_lut);
c168a833 341
6d2f911b 342 httpClose(cg->http);
ef416fc2 343
7d5824d6 344#ifdef HAVE_SSL
7cf5915e 345 _httpFreeCredentials(cg->tls_credentials);
7d5824d6 346#endif /* HAVE_SSL */
7cf5915e 347
6d2f911b
MS
348 cupsFileClose(cg->stdio_files[0]);
349 cupsFileClose(cg->stdio_files[1]);
350 cupsFileClose(cg->stdio_files[2]);
ef416fc2 351
6d2f911b 352 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
ef416fc2 353
dd204f7a
MS
354 if (cg->raster_error.start)
355 free(cg->raster_error.start);
356
6d2f911b
MS
357 free(cg);
358}
19dc16f7 359#endif /* HAVE_PTHREAD_H || _WIN32 */
6d2f911b
MS
360
361
362#ifdef HAVE_PTHREAD_H
ef416fc2 363/*
6d2f911b 364 * 'cups_globals_init()' - Initialize environment variables.
ef416fc2 365 */
366
6d2f911b
MS
367static void
368cups_globals_init(void)
ef416fc2 369{
ef416fc2 370 /*
6d2f911b 371 * Register the global data for this thread...
ef416fc2 372 */
373
6d2f911b 374 pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free);
ef416fc2 375}
376#endif /* HAVE_PTHREAD_H */