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