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