]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
bc44d920 | 2 | * "$Id: globals.c 6649 2007-07-11 21:46:42Z mike $" |
ef416fc2 | 3 | * |
4 | * Global variable access routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
bc44d920 | 6 | * Copyright 2007 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 | ||
29 | #include "http-private.h" | |
30 | #include "globals.h" | |
d6ae789d | 31 | #include "debug.h" |
ef416fc2 | 32 | #include <stdlib.h> |
33 | ||
34 | ||
35 | /* | |
36 | * 'cups_env_init()' - Initialize environment variables. | |
37 | */ | |
38 | ||
39 | static void | |
40 | cups_env_init(_cups_globals_t *g) /* I - Global data */ | |
41 | { | |
42 | if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL) | |
43 | g->cups_datadir = CUPS_DATADIR; | |
44 | ||
45 | if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) | |
46 | g->cups_serverbin = CUPS_SERVERBIN; | |
47 | ||
48 | if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
49 | g->cups_serverroot = CUPS_SERVERROOT; | |
50 | ||
51 | if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) | |
52 | g->cups_statedir = CUPS_STATEDIR; | |
53 | ||
54 | if ((g->localedir = getenv("LOCALEDIR")) == NULL) | |
55 | g->localedir = CUPS_LOCALEDIR; | |
56 | } | |
57 | ||
58 | ||
59 | #ifdef HAVE_PTHREAD_H | |
60 | /* | |
61 | * Implement per-thread globals... | |
62 | */ | |
63 | ||
64 | /* | |
65 | * Local globals... | |
66 | */ | |
67 | ||
68 | static pthread_key_t globals_key = -1; | |
69 | /* Thread local storage key */ | |
70 | static pthread_once_t globals_key_once = PTHREAD_ONCE_INIT; | |
71 | /* One-time initialization object */ | |
72 | ||
73 | ||
74 | /* | |
75 | * Local functions... | |
76 | */ | |
77 | ||
78 | static void globals_init(); | |
79 | static void globals_destructor(void *value); | |
80 | ||
81 | ||
82 | /* | |
83 | * '_cupsGlobals()' - Return a pointer to thread local storage | |
84 | */ | |
85 | ||
86 | _cups_globals_t * /* O - Pointer to global data */ | |
87 | _cupsGlobals(void) | |
88 | { | |
89 | _cups_globals_t *globals; /* Pointer to global data */ | |
90 | ||
91 | ||
92 | /* | |
93 | * Initialize the global data exactly once... | |
94 | */ | |
95 | ||
d6ae789d | 96 | DEBUG_printf(("_cupsGlobals(): globals_key_once=%d\n", globals_key_once)); |
97 | ||
ef416fc2 | 98 | pthread_once(&globals_key_once, globals_init); |
99 | ||
100 | /* | |
101 | * See if we have allocated the data yet... | |
102 | */ | |
103 | ||
104 | if ((globals = (_cups_globals_t *)pthread_getspecific(globals_key)) == NULL) | |
105 | { | |
d6ae789d | 106 | DEBUG_puts("_cupsGlobals: allocating memory for thread..."); |
107 | ||
ef416fc2 | 108 | /* |
109 | * No, allocate memory as set the pointer for the key... | |
110 | */ | |
111 | ||
112 | globals = calloc(1, sizeof(_cups_globals_t)); | |
113 | pthread_setspecific(globals_key, globals); | |
114 | ||
d6ae789d | 115 | DEBUG_printf((" globals=%p\n", globals)); |
116 | ||
ef416fc2 | 117 | /* |
118 | * Initialize variables that have non-zero values | |
119 | */ | |
120 | ||
121 | globals->encryption = (http_encryption_t)-1; | |
122 | globals->password_cb = _cupsGetPassword; | |
123 | ||
124 | cups_env_init(globals); | |
125 | } | |
126 | ||
127 | /* | |
128 | * Return the pointer to the data... | |
129 | */ | |
130 | ||
131 | return (globals); | |
132 | } | |
133 | ||
134 | ||
135 | /* | |
136 | * 'globals_init()' - Initialize globals once. | |
137 | */ | |
138 | ||
139 | static void | |
140 | globals_init() | |
141 | { | |
142 | pthread_key_create(&globals_key, globals_destructor); | |
d6ae789d | 143 | |
144 | DEBUG_printf(("globals_init(): globals_key=%x(%u)\n", globals_key, | |
145 | globals_key)); | |
ef416fc2 | 146 | } |
147 | ||
148 | ||
149 | /* | |
150 | * 'globals_destructor()' - Free memory allocated by _cupsGlobals(). | |
151 | */ | |
152 | ||
153 | static void | |
154 | globals_destructor(void *value) /* I - Data to free */ | |
155 | { | |
80ca4592 | 156 | int i; /* Looping var */ |
fa73b229 | 157 | _cups_globals_t *cg; /* Global data */ |
158 | ||
159 | ||
d6ae789d | 160 | DEBUG_printf(("globals_destructor(value=%p)\n", value)); |
161 | ||
fa73b229 | 162 | cg = (_cups_globals_t *)value; |
163 | ||
80ca4592 | 164 | httpClose(cg->http); |
165 | ||
166 | for (i = 0; i < 3; i ++) | |
167 | cupsFileClose(cg->stdio_files[i]); | |
fa73b229 | 168 | |
e53920b9 | 169 | if (cg->last_status_message) |
170 | free(cg->last_status_message); | |
171 | ||
757d2cad | 172 | cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); |
173 | ||
ef416fc2 | 174 | free(value); |
175 | } | |
176 | ||
177 | ||
178 | #else | |
179 | /* | |
180 | * Implement static globals... | |
181 | */ | |
182 | ||
183 | /* | |
184 | * '_cupsGlobals()' - Return a pointer to thread local storage. | |
185 | */ | |
186 | ||
187 | _cups_globals_t * /* O - Pointer to global data */ | |
188 | _cupsGlobals(void) | |
189 | { | |
190 | static _cups_globals_t globals; /* Global data */ | |
191 | static int initialized = 0;/* Global data initialized? */ | |
192 | ||
193 | ||
194 | /* | |
195 | * Initialize global data as needed... | |
196 | */ | |
197 | ||
198 | if (!initialized) | |
199 | { | |
200 | initialized = 1; | |
201 | ||
202 | /* | |
203 | * Initialize global variables... | |
204 | */ | |
205 | ||
206 | memset(&globals, 0, sizeof(globals)); | |
207 | ||
208 | globals.encryption = (http_encryption_t)-1; | |
209 | globals.password_cb = _cupsGetPassword; | |
210 | ||
211 | cups_env_init(&globals); | |
212 | } | |
213 | ||
214 | return (&globals); | |
215 | } | |
216 | #endif /* HAVE_PTHREAD_H */ | |
217 | ||
218 | ||
219 | /* | |
bc44d920 | 220 | * End of "$Id: globals.c 6649 2007-07-11 21:46:42Z mike $". |
ef416fc2 | 221 | */ |