2 * "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $"
4 * Global variable access routines for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
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().
26 * Include necessary headers...
29 #include "cups-private.h"
33 * 'cups_env_init()' - Initialize environment variables.
37 cups_env_init(_cups_globals_t
*g
) /* I - Global data */
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 */
48 * Open the registry...
51 strcpy(installdir
, "C:/Program Files/cups.org");
53 if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE
, "SOFTWARE\\cups.org", 0, KEY_READ
,
57 * Grab the installation directory...
60 size
= sizeof(installdir
);
61 RegQueryValueEx(key
, "installdir", NULL
, NULL
, installdir
, &size
);
65 snprintf(confdir
, sizeof(confdir
), "%s/conf", installdir
);
66 snprintf(localedir
, sizeof(localedir
), "%s/locale", installdir
);
68 if ((g
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
69 g
->cups_datadir
= installdir
;
71 if ((g
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
72 g
->cups_serverbin
= installdir
;
74 if ((g
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
75 g
->cups_serverroot
= confdir
;
77 if ((g
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
78 g
->cups_statedir
= confdir
;
80 if ((g
->localedir
= getenv("LOCALEDIR")) == NULL
)
81 g
->localedir
= localedir
;
85 if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
88 # endif /* HAVE_GETEUID */
91 * When running setuid/setgid, don't allow environment variables to override
95 g
->cups_datadir
= CUPS_DATADIR
;
96 g
->cups_serverbin
= CUPS_SERVERBIN
;
97 g
->cups_serverroot
= CUPS_SERVERROOT
;
98 g
->cups_statedir
= CUPS_STATEDIR
;
99 g
->localedir
= CUPS_LOCALEDIR
;
104 * Allow directories to be overridden by environment variables.
107 if ((g
->cups_datadir
= getenv("CUPS_DATADIR")) == NULL
)
108 g
->cups_datadir
= CUPS_DATADIR
;
110 if ((g
->cups_serverbin
= getenv("CUPS_SERVERBIN")) == NULL
)
111 g
->cups_serverbin
= CUPS_SERVERBIN
;
113 if ((g
->cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
114 g
->cups_serverroot
= CUPS_SERVERROOT
;
116 if ((g
->cups_statedir
= getenv("CUPS_STATEDIR")) == NULL
)
117 g
->cups_statedir
= CUPS_STATEDIR
;
119 if ((g
->localedir
= getenv("LOCALEDIR")) == NULL
)
120 g
->localedir
= CUPS_LOCALEDIR
;
126 #ifdef HAVE_PTHREAD_H
128 * Implement per-thread globals...
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 */
145 static void globals_init();
146 static void globals_destructor(void *value
);
150 * '_cupsGlobals()' - Return a pointer to thread local storage
153 _cups_globals_t
* /* O - Pointer to global data */
156 _cups_globals_t
*globals
; /* Pointer to global data */
160 * Initialize the global data exactly once...
163 pthread_once(&globals_key_once
, globals_init
);
166 * See if we have allocated the data yet...
169 if ((globals
= (_cups_globals_t
*)pthread_getspecific(globals_key
)) == NULL
)
172 * No, allocate memory as set the pointer for the key...
175 globals
= calloc(1, sizeof(_cups_globals_t
));
176 pthread_setspecific(globals_key
, globals
);
179 * Initialize variables that have non-zero values
182 globals
->encryption
= (http_encryption_t
)-1;
183 globals
->password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
185 cups_env_init(globals
);
189 * Return the pointer to the data...
197 * 'globals_init()' - Initialize globals once.
203 pthread_key_create(&globals_key
, globals_destructor
);
208 * 'globals_destructor()' - Free memory allocated by _cupsGlobals().
212 globals_destructor(void *value
) /* I - Data to free */
214 int i
; /* Looping var */
215 _ipp_buffer_t
*buffer
, /* Current IPP read/write buffer */
216 *next
; /* Next buffer */
217 _cups_globals_t
*cg
; /* Global data */
220 cg
= (_cups_globals_t
*)value
;
224 for (i
= 0; i
< 3; i
++)
225 cupsFileClose(cg
->stdio_files
[i
]);
227 if (cg
->last_status_message
)
228 _cupsStrFree(cg
->last_status_message
);
230 cupsFreeOptions(cg
->cupsd_num_settings
, cg
->cupsd_settings
);
232 for (buffer
= cg
->ipp_buffers
; buffer
; buffer
= next
)
238 cupsArrayDelete(cg
->pwg_size_lut
);
239 cupsArrayDelete(cg
->leg_size_lut
);
247 * Implement static globals...
251 * '_cupsGlobals()' - Return a pointer to thread local storage.
254 _cups_globals_t
* /* O - Pointer to global data */
257 static _cups_globals_t globals
; /* Global data */
258 static int initialized
= 0;/* Global data initialized? */
262 * Initialize global data as needed...
270 * Initialize global variables...
273 memset(&globals
, 0, sizeof(globals
));
275 globals
.encryption
= (http_encryption_t
)-1;
276 globals
.password_cb
= (cups_password_cb2_t
)_cupsGetPassword
;
278 cups_env_init(&globals
);
283 #endif /* HAVE_PTHREAD_H */
287 * End of "$Id: globals.c 7870 2008-08-27 18:14:10Z mike $".