]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $" |
ef416fc2 | 3 | * |
71e16022 | 4 | * Global variable access routines for CUPS. |
ef416fc2 | 5 | * |
71e16022 | 6 | * Copyright 2007-2010 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 | * | |
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 | ||
71e16022 | 29 | #include "cups-private.h" |
ef416fc2 | 30 | |
31 | ||
32 | /* | |
33 | * 'cups_env_init()' - Initialize environment variables. | |
34 | */ | |
35 | ||
36 | static void | |
37 | cups_env_init(_cups_globals_t *g) /* I - Global data */ | |
38 | { | |
f8b3a85b MS |
39 | #ifdef WIN32 |
40 | HKEY key; /* Registry key */ | |
41 | DWORD size; /* Size of string */ | |
42 | static char installdir[1024], /* Install directory */ | |
43 | confdir[1024], /* Server root directory */ | |
44 | localedir[1024]; /* Locale directory */ | |
45 | ||
46 | ||
47 | /* | |
48 | * Open the registry... | |
49 | */ | |
50 | ||
51 | strcpy(installdir, "C:/Program Files/cups.org"); | |
52 | ||
53 | if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, | |
54 | &key)) | |
55 | { | |
56 | /* | |
57 | * Grab the installation directory... | |
58 | */ | |
59 | ||
60 | size = sizeof(installdir); | |
61 | RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size); | |
62 | RegCloseKey(key); | |
63 | } | |
64 | ||
65 | snprintf(confdir, sizeof(confdir), "%s/conf", installdir); | |
66 | snprintf(localedir, sizeof(localedir), "%s/locale", installdir); | |
67 | ||
68 | if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL) | |
69 | g->cups_datadir = installdir; | |
70 | ||
71 | if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) | |
72 | g->cups_serverbin = installdir; | |
73 | ||
74 | if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
75 | g->cups_serverroot = confdir; | |
76 | ||
77 | if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) | |
78 | g->cups_statedir = confdir; | |
79 | ||
80 | if ((g->localedir = getenv("LOCALEDIR")) == NULL) | |
81 | g->localedir = localedir; | |
82 | ||
83 | #else | |
5180a04c MS |
84 | # ifdef HAVE_GETEUID |
85 | if ((geteuid() != getuid() && getuid()) || getegid() != getgid()) | |
86 | # else | |
87 | if (!getuid()) | |
88 | # endif /* HAVE_GETEUID */ | |
89 | { | |
90 | /* | |
91 | * When running setuid/setgid, don't allow environment variables to override | |
92 | * the directories... | |
93 | */ | |
ef416fc2 | 94 | |
5180a04c MS |
95 | g->cups_datadir = CUPS_DATADIR; |
96 | g->cups_serverbin = CUPS_SERVERBIN; | |
ef416fc2 | 97 | g->cups_serverroot = CUPS_SERVERROOT; |
5180a04c MS |
98 | g->cups_statedir = CUPS_STATEDIR; |
99 | g->localedir = CUPS_LOCALEDIR; | |
100 | } | |
101 | else | |
102 | { | |
103 | /* | |
104 | * Allow directories to be overridden by environment variables. | |
105 | */ | |
ef416fc2 | 106 | |
5180a04c MS |
107 | if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL) |
108 | g->cups_datadir = CUPS_DATADIR; | |
ef416fc2 | 109 | |
5180a04c MS |
110 | if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) |
111 | g->cups_serverbin = CUPS_SERVERBIN; | |
112 | ||
113 | if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
114 | g->cups_serverroot = CUPS_SERVERROOT; | |
115 | ||
116 | if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) | |
117 | g->cups_statedir = CUPS_STATEDIR; | |
118 | ||
119 | if ((g->localedir = getenv("LOCALEDIR")) == NULL) | |
120 | g->localedir = CUPS_LOCALEDIR; | |
121 | } | |
f8b3a85b | 122 | #endif /* WIN32 */ |
ef416fc2 | 123 | } |
124 | ||
125 | ||
126 | #ifdef HAVE_PTHREAD_H | |
127 | /* | |
128 | * Implement per-thread globals... | |
129 | */ | |
130 | ||
131 | /* | |
132 | * Local globals... | |
133 | */ | |
134 | ||
135 | static pthread_key_t globals_key = -1; | |
136 | /* Thread local storage key */ | |
137 | static pthread_once_t globals_key_once = PTHREAD_ONCE_INIT; | |
138 | /* One-time initialization object */ | |
139 | ||
140 | ||
141 | /* | |
142 | * Local functions... | |
143 | */ | |
144 | ||
145 | static void globals_init(); | |
146 | static void globals_destructor(void *value); | |
147 | ||
148 | ||
149 | /* | |
150 | * '_cupsGlobals()' - Return a pointer to thread local storage | |
151 | */ | |
152 | ||
153 | _cups_globals_t * /* O - Pointer to global data */ | |
154 | _cupsGlobals(void) | |
155 | { | |
156 | _cups_globals_t *globals; /* Pointer to global data */ | |
157 | ||
158 | ||
5180a04c | 159 | /* |
ef416fc2 | 160 | * Initialize the global data exactly once... |
161 | */ | |
162 | ||
163 | pthread_once(&globals_key_once, globals_init); | |
164 | ||
165 | /* | |
166 | * See if we have allocated the data yet... | |
167 | */ | |
168 | ||
169 | if ((globals = (_cups_globals_t *)pthread_getspecific(globals_key)) == NULL) | |
170 | { | |
171 | /* | |
172 | * No, allocate memory as set the pointer for the key... | |
173 | */ | |
174 | ||
175 | globals = calloc(1, sizeof(_cups_globals_t)); | |
176 | pthread_setspecific(globals_key, globals); | |
177 | ||
178 | /* | |
179 | * Initialize variables that have non-zero values | |
180 | */ | |
181 | ||
182 | globals->encryption = (http_encryption_t)-1; | |
f11a948a | 183 | globals->password_cb = (cups_password_cb2_t)_cupsGetPassword; |
ef416fc2 | 184 | |
185 | cups_env_init(globals); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Return the pointer to the data... | |
190 | */ | |
191 | ||
192 | return (globals); | |
193 | } | |
194 | ||
195 | ||
196 | /* | |
197 | * 'globals_init()' - Initialize globals once. | |
198 | */ | |
199 | ||
200 | static void | |
201 | globals_init() | |
202 | { | |
203 | pthread_key_create(&globals_key, globals_destructor); | |
204 | } | |
205 | ||
206 | ||
207 | /* | |
208 | * 'globals_destructor()' - Free memory allocated by _cupsGlobals(). | |
209 | */ | |
210 | ||
211 | static void | |
212 | globals_destructor(void *value) /* I - Data to free */ | |
213 | { | |
80ca4592 | 214 | int i; /* Looping var */ |
1f6f3dbc MS |
215 | _ipp_buffer_t *buffer, /* Current IPP read/write buffer */ |
216 | *next; /* Next buffer */ | |
fa73b229 | 217 | _cups_globals_t *cg; /* Global data */ |
218 | ||
219 | ||
220 | cg = (_cups_globals_t *)value; | |
221 | ||
80ca4592 | 222 | httpClose(cg->http); |
223 | ||
224 | for (i = 0; i < 3; i ++) | |
225 | cupsFileClose(cg->stdio_files[i]); | |
fa73b229 | 226 | |
e53920b9 | 227 | if (cg->last_status_message) |
5f64df29 | 228 | _cupsStrFree(cg->last_status_message); |
e53920b9 | 229 | |
757d2cad | 230 | cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); |
231 | ||
1f6f3dbc MS |
232 | for (buffer = cg->ipp_buffers; buffer; buffer = next) |
233 | { | |
234 | next = buffer->next; | |
235 | free(buffer); | |
236 | } | |
237 | ||
c168a833 MS |
238 | cupsArrayDelete(cg->pwg_size_lut); |
239 | cupsArrayDelete(cg->leg_size_lut); | |
240 | ||
ef416fc2 | 241 | free(value); |
242 | } | |
243 | ||
244 | ||
245 | #else | |
246 | /* | |
247 | * Implement static globals... | |
248 | */ | |
249 | ||
250 | /* | |
251 | * '_cupsGlobals()' - Return a pointer to thread local storage. | |
252 | */ | |
253 | ||
254 | _cups_globals_t * /* O - Pointer to global data */ | |
255 | _cupsGlobals(void) | |
256 | { | |
257 | static _cups_globals_t globals; /* Global data */ | |
258 | static int initialized = 0;/* Global data initialized? */ | |
259 | ||
260 | ||
261 | /* | |
262 | * Initialize global data as needed... | |
263 | */ | |
264 | ||
265 | if (!initialized) | |
266 | { | |
267 | initialized = 1; | |
268 | ||
269 | /* | |
270 | * Initialize global variables... | |
271 | */ | |
272 | ||
273 | memset(&globals, 0, sizeof(globals)); | |
274 | ||
275 | globals.encryption = (http_encryption_t)-1; | |
536bc2c6 | 276 | globals.password_cb = (cups_password_cb2_t)_cupsGetPassword; |
ef416fc2 | 277 | |
278 | cups_env_init(&globals); | |
279 | } | |
280 | ||
281 | return (&globals); | |
282 | } | |
283 | #endif /* HAVE_PTHREAD_H */ | |
284 | ||
285 | ||
286 | /* | |
b19ccc9e | 287 | * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $". |
ef416fc2 | 288 | */ |