]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/globals.c
Merge changes from CUPS 1.5svn-r9000.
[thirdparty/cups.git] / cups / globals.c
1 /*
2 * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $"
3 *
4 * Global variable access routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * This file is subject to the Apple OS-Developed Software exception.
16 *
17 * Contents:
18 *
19 * _cupsGlobals() - Return a pointer to thread local storage.
20 * cups_env_init() - Initialize environment variables.
21 * globals_init() - Initialize globals once.
22 * globals_destructor() - Free memory allocated by _cupsGlobals().
23 */
24
25 /*
26 * Include necessary headers...
27 */
28
29 #include "http-private.h"
30 #include "globals.h"
31 #include <stdlib.h>
32
33
34 /*
35 * 'cups_env_init()' - Initialize environment variables.
36 */
37
38 static void
39 cups_env_init(_cups_globals_t *g) /* I - Global data */
40 {
41 #ifdef WIN32
42 HKEY key; /* Registry key */
43 DWORD size; /* Size of string */
44 static char installdir[1024], /* Install directory */
45 confdir[1024], /* Server root directory */
46 localedir[1024]; /* Locale directory */
47
48
49 /*
50 * Open the registry...
51 */
52
53 strcpy(installdir, "C:/Program Files/cups.org");
54
55 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ,
56 &key))
57 {
58 /*
59 * Grab the installation directory...
60 */
61
62 size = sizeof(installdir);
63 RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size);
64 RegCloseKey(key);
65 }
66
67 snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
68 snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
69
70 if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
71 g->cups_datadir = installdir;
72
73 if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
74 g->cups_serverbin = installdir;
75
76 if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
77 g->cups_serverroot = confdir;
78
79 if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
80 g->cups_statedir = confdir;
81
82 if ((g->localedir = getenv("LOCALEDIR")) == NULL)
83 g->localedir = localedir;
84
85 #else
86 if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
87 g->cups_datadir = CUPS_DATADIR;
88
89 if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL)
90 g->cups_serverbin = CUPS_SERVERBIN;
91
92 if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL)
93 g->cups_serverroot = CUPS_SERVERROOT;
94
95 if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL)
96 g->cups_statedir = CUPS_STATEDIR;
97
98 if ((g->localedir = getenv("LOCALEDIR")) == NULL)
99 g->localedir = CUPS_LOCALEDIR;
100 #endif /* WIN32 */
101 }
102
103
104 #ifdef HAVE_PTHREAD_H
105 /*
106 * Implement per-thread globals...
107 */
108
109 /*
110 * Local globals...
111 */
112
113 static pthread_key_t globals_key = -1;
114 /* Thread local storage key */
115 static pthread_once_t globals_key_once = PTHREAD_ONCE_INIT;
116 /* One-time initialization object */
117
118
119 /*
120 * Local functions...
121 */
122
123 static void globals_init();
124 static void globals_destructor(void *value);
125
126
127 /*
128 * '_cupsGlobals()' - Return a pointer to thread local storage
129 */
130
131 _cups_globals_t * /* O - Pointer to global data */
132 _cupsGlobals(void)
133 {
134 _cups_globals_t *globals; /* Pointer to global data */
135
136
137 /*
138 * Initialize the global data exactly once...
139 */
140
141 pthread_once(&globals_key_once, globals_init);
142
143 /*
144 * See if we have allocated the data yet...
145 */
146
147 if ((globals = (_cups_globals_t *)pthread_getspecific(globals_key)) == NULL)
148 {
149 /*
150 * No, allocate memory as set the pointer for the key...
151 */
152
153 globals = calloc(1, sizeof(_cups_globals_t));
154 pthread_setspecific(globals_key, globals);
155
156 /*
157 * Initialize variables that have non-zero values
158 */
159
160 globals->encryption = (http_encryption_t)-1;
161 globals->password_cb = (cups_password_cb2_t)_cupsGetPassword;
162
163 cups_env_init(globals);
164 }
165
166 /*
167 * Return the pointer to the data...
168 */
169
170 return (globals);
171 }
172
173
174 /*
175 * 'globals_init()' - Initialize globals once.
176 */
177
178 static void
179 globals_init()
180 {
181 pthread_key_create(&globals_key, globals_destructor);
182 }
183
184
185 /*
186 * 'globals_destructor()' - Free memory allocated by _cupsGlobals().
187 */
188
189 static void
190 globals_destructor(void *value) /* I - Data to free */
191 {
192 int i; /* Looping var */
193 _ipp_buffer_t *buffer, /* Current IPP read/write buffer */
194 *next; /* Next buffer */
195 _cups_globals_t *cg; /* Global data */
196
197
198 cg = (_cups_globals_t *)value;
199
200 httpClose(cg->http);
201
202 for (i = 0; i < 3; i ++)
203 cupsFileClose(cg->stdio_files[i]);
204
205 if (cg->last_status_message)
206 _cupsStrFree(cg->last_status_message);
207
208 cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
209
210 for (buffer = cg->ipp_buffers; buffer; buffer = next)
211 {
212 next = buffer->next;
213 free(buffer);
214 }
215
216 cupsArrayDelete(cg->pwg_size_lut);
217 cupsArrayDelete(cg->leg_size_lut);
218
219 free(value);
220 }
221
222
223 #else
224 /*
225 * Implement static globals...
226 */
227
228 /*
229 * '_cupsGlobals()' - Return a pointer to thread local storage.
230 */
231
232 _cups_globals_t * /* O - Pointer to global data */
233 _cupsGlobals(void)
234 {
235 static _cups_globals_t globals; /* Global data */
236 static int initialized = 0;/* Global data initialized? */
237
238
239 /*
240 * Initialize global data as needed...
241 */
242
243 if (!initialized)
244 {
245 initialized = 1;
246
247 /*
248 * Initialize global variables...
249 */
250
251 memset(&globals, 0, sizeof(globals));
252
253 globals.encryption = (http_encryption_t)-1;
254 globals.password_cb = (cups_password_cb2_t)_cupsGetPassword;
255
256 cups_env_init(&globals);
257 }
258
259 return (&globals);
260 }
261 #endif /* HAVE_PTHREAD_H */
262
263
264 /*
265 * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $".
266 */