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