]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/globals.c
Greatly simplify the man page handling.
[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 (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, &key))
226 {
227 /*
228 * Grab the installation directory...
229 */
230
231 char *ptr; /* Pointer into installdir */
232
233 size = sizeof(installdir);
234 RegQueryValueExA(key, "installdir", NULL, NULL, installdir, &size);
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);
255 }
256
257 if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
258 cg->cups_datadir = installdir;
259
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 {
279 /*
280 * When running setuid/setgid, don't allow environment variables to override
281 * the directories...
282 */
283
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;
289 }
290 else
291 {
292 /*
293 * Allow directories to be overridden by environment variables.
294 */
295
296 if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
297 cg->cups_datadir = CUPS_DATADIR;
298
299 if ((cg->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
300 cg->cups_serverbin = CUPS_SERVERBIN;
301
302 if ((cg->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
303 cg->cups_serverroot = CUPS_SERVERROOT;
304
305 if ((cg->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
306 cg->cups_statedir = CUPS_STATEDIR;
307
308 if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
309 cg->localedir = CUPS_LOCALEDIR;
310 }
311 #endif /* _WIN32 */
312
313 return (cg);
314 }
315
316
317 /*
318 * 'cups_globals_free()' - Free global data.
319 */
320
321 #if defined(HAVE_PTHREAD_H) || defined(_WIN32)
322 static void
323 cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */
324 {
325 _cups_buffer_t *buffer, /* Current read/write buffer */
326 *next; /* Next buffer */
327
328
329 if (cg->last_status_message)
330 _cupsStrFree(cg->last_status_message);
331
332 for (buffer = cg->cups_buffers; buffer; buffer = next)
333 {
334 next = buffer->next;
335 free(buffer);
336 }
337
338 cupsArrayDelete(cg->leg_size_lut);
339 cupsArrayDelete(cg->ppd_size_lut);
340 cupsArrayDelete(cg->pwg_size_lut);
341
342 httpClose(cg->http);
343
344 #ifdef HAVE_SSL
345 _httpFreeCredentials(cg->tls_credentials);
346 #endif /* HAVE_SSL */
347
348 cupsFileClose(cg->stdio_files[0]);
349 cupsFileClose(cg->stdio_files[1]);
350 cupsFileClose(cg->stdio_files[2]);
351
352 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
353
354 if (cg->raster_error.start)
355 free(cg->raster_error.start);
356
357 free(cg);
358 }
359 #endif /* HAVE_PTHREAD_H || _WIN32 */
360
361
362 #ifdef HAVE_PTHREAD_H
363 /*
364 * 'cups_globals_init()' - Initialize environment variables.
365 */
366
367 static void
368 cups_globals_init(void)
369 {
370 /*
371 * Register the global data for this thread...
372 */
373
374 pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free);
375 }
376 #endif /* HAVE_PTHREAD_H */