]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f7faf1f5 | 2 | * "$Id: globals.c 5373 2006-04-06 20:03:32Z mike $" |
ef416fc2 | 3 | * |
4 | * Global variable access routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
28 | * _cupsGlobals() - Return a pointer to thread local storage. | |
29 | * cups_env_init() - Initialize environment variables. | |
30 | * globals_init() - Initialize globals once. | |
31 | * globals_destructor() - Free memory allocated by _cupsGlobals(). | |
32 | */ | |
33 | ||
34 | /* | |
35 | * Include necessary headers... | |
36 | */ | |
37 | ||
38 | #include "http-private.h" | |
39 | #include "globals.h" | |
d6ae789d | 40 | #include "debug.h" |
ef416fc2 | 41 | #include <stdlib.h> |
42 | ||
43 | ||
44 | /* | |
45 | * 'cups_env_init()' - Initialize environment variables. | |
46 | */ | |
47 | ||
48 | static void | |
49 | cups_env_init(_cups_globals_t *g) /* I - Global data */ | |
50 | { | |
51 | if ((g->cups_datadir = getenv("CUPS_DATADIR")) == NULL) | |
52 | g->cups_datadir = CUPS_DATADIR; | |
53 | ||
54 | if ((g->cups_serverbin = getenv("CUPS_SERVERBIN")) == NULL) | |
55 | g->cups_serverbin = CUPS_SERVERBIN; | |
56 | ||
57 | if ((g->cups_serverroot = getenv("CUPS_SERVERROOT")) == NULL) | |
58 | g->cups_serverroot = CUPS_SERVERROOT; | |
59 | ||
60 | if ((g->cups_statedir = getenv("CUPS_STATEDIR")) == NULL) | |
61 | g->cups_statedir = CUPS_STATEDIR; | |
62 | ||
63 | if ((g->localedir = getenv("LOCALEDIR")) == NULL) | |
64 | g->localedir = CUPS_LOCALEDIR; | |
65 | } | |
66 | ||
67 | ||
68 | #ifdef HAVE_PTHREAD_H | |
69 | /* | |
70 | * Implement per-thread globals... | |
71 | */ | |
72 | ||
73 | /* | |
74 | * Local globals... | |
75 | */ | |
76 | ||
77 | static pthread_key_t globals_key = -1; | |
78 | /* Thread local storage key */ | |
79 | static pthread_once_t globals_key_once = PTHREAD_ONCE_INIT; | |
80 | /* One-time initialization object */ | |
81 | ||
82 | ||
83 | /* | |
84 | * Local functions... | |
85 | */ | |
86 | ||
87 | static void globals_init(); | |
88 | static void globals_destructor(void *value); | |
89 | ||
90 | ||
91 | /* | |
92 | * '_cupsGlobals()' - Return a pointer to thread local storage | |
93 | */ | |
94 | ||
95 | _cups_globals_t * /* O - Pointer to global data */ | |
96 | _cupsGlobals(void) | |
97 | { | |
98 | _cups_globals_t *globals; /* Pointer to global data */ | |
99 | ||
100 | ||
101 | /* | |
102 | * Initialize the global data exactly once... | |
103 | */ | |
104 | ||
d6ae789d | 105 | DEBUG_printf(("_cupsGlobals(): globals_key_once=%d\n", globals_key_once)); |
106 | ||
ef416fc2 | 107 | pthread_once(&globals_key_once, globals_init); |
108 | ||
109 | /* | |
110 | * See if we have allocated the data yet... | |
111 | */ | |
112 | ||
113 | if ((globals = (_cups_globals_t *)pthread_getspecific(globals_key)) == NULL) | |
114 | { | |
d6ae789d | 115 | DEBUG_puts("_cupsGlobals: allocating memory for thread..."); |
116 | ||
ef416fc2 | 117 | /* |
118 | * No, allocate memory as set the pointer for the key... | |
119 | */ | |
120 | ||
121 | globals = calloc(1, sizeof(_cups_globals_t)); | |
122 | pthread_setspecific(globals_key, globals); | |
123 | ||
d6ae789d | 124 | DEBUG_printf((" globals=%p\n", globals)); |
125 | ||
ef416fc2 | 126 | /* |
127 | * Initialize variables that have non-zero values | |
128 | */ | |
129 | ||
130 | globals->encryption = (http_encryption_t)-1; | |
131 | globals->password_cb = _cupsGetPassword; | |
132 | ||
133 | cups_env_init(globals); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Return the pointer to the data... | |
138 | */ | |
139 | ||
140 | return (globals); | |
141 | } | |
142 | ||
143 | ||
144 | /* | |
145 | * 'globals_init()' - Initialize globals once. | |
146 | */ | |
147 | ||
148 | static void | |
149 | globals_init() | |
150 | { | |
151 | pthread_key_create(&globals_key, globals_destructor); | |
d6ae789d | 152 | |
153 | DEBUG_printf(("globals_init(): globals_key=%x(%u)\n", globals_key, | |
154 | globals_key)); | |
ef416fc2 | 155 | } |
156 | ||
157 | ||
158 | /* | |
159 | * 'globals_destructor()' - Free memory allocated by _cupsGlobals(). | |
160 | */ | |
161 | ||
162 | static void | |
163 | globals_destructor(void *value) /* I - Data to free */ | |
164 | { | |
80ca4592 | 165 | int i; /* Looping var */ |
fa73b229 | 166 | _cups_globals_t *cg; /* Global data */ |
167 | ||
168 | ||
d6ae789d | 169 | DEBUG_printf(("globals_destructor(value=%p)\n", value)); |
170 | ||
fa73b229 | 171 | cg = (_cups_globals_t *)value; |
172 | ||
80ca4592 | 173 | httpClose(cg->http); |
174 | ||
175 | for (i = 0; i < 3; i ++) | |
176 | cupsFileClose(cg->stdio_files[i]); | |
fa73b229 | 177 | |
e53920b9 | 178 | if (cg->last_status_message) |
179 | free(cg->last_status_message); | |
180 | ||
757d2cad | 181 | cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings); |
182 | ||
ef416fc2 | 183 | free(value); |
184 | } | |
185 | ||
186 | ||
187 | #else | |
188 | /* | |
189 | * Implement static globals... | |
190 | */ | |
191 | ||
192 | /* | |
193 | * '_cupsGlobals()' - Return a pointer to thread local storage. | |
194 | */ | |
195 | ||
196 | _cups_globals_t * /* O - Pointer to global data */ | |
197 | _cupsGlobals(void) | |
198 | { | |
199 | static _cups_globals_t globals; /* Global data */ | |
200 | static int initialized = 0;/* Global data initialized? */ | |
201 | ||
202 | ||
203 | /* | |
204 | * Initialize global data as needed... | |
205 | */ | |
206 | ||
207 | if (!initialized) | |
208 | { | |
209 | initialized = 1; | |
210 | ||
211 | /* | |
212 | * Initialize global variables... | |
213 | */ | |
214 | ||
215 | memset(&globals, 0, sizeof(globals)); | |
216 | ||
217 | globals.encryption = (http_encryption_t)-1; | |
218 | globals.password_cb = _cupsGetPassword; | |
219 | ||
220 | cups_env_init(&globals); | |
221 | } | |
222 | ||
223 | return (&globals); | |
224 | } | |
225 | #endif /* HAVE_PTHREAD_H */ | |
226 | ||
227 | ||
228 | /* | |
f7faf1f5 | 229 | * End of "$Id: globals.c 5373 2006-04-06 20:03:32Z mike $". |
ef416fc2 | 230 | */ |