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