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