/*
- * "$Id: globals.c 10436 2012-04-23 21:52:02Z mike $"
+ * Global variable access routines for CUPS.
*
- * Global variable access routines for CUPS.
+ * Copyright © 2007-2019 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products, all rights reserved.
*
- * Copyright 2007-2010 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products, all rights reserved.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law. Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file. If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- * _cupsGlobalLock() - Lock the global mutex.
- * _cupsGlobals() - Return a pointer to thread local storage
- * _cupsGlobalUnlock() - Unlock the global mutex.
- * DllMain() - Main entry for library.
- * cups_globals_alloc() - Allocate and initialize global data.
- * cups_globals_free() - Free global data.
- * cups_globals_init() - Initialize environment variables.
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more
+ * information.
*/
/*
*/
#include "cups-private.h"
+#ifndef _WIN32
+# include <pwd.h>
+#endif /* !_WIN32 */
/*
* Local globals...
*/
-
+#ifdef DEBUG
+static int cups_global_index = 0;
+ /* Next thread number */
+#endif /* DEBUG */
static _cups_threadkey_t cups_globals_key = _CUPS_THREADKEY_INITIALIZER;
/* Thread local storage key */
#ifdef HAVE_PTHREAD_H
static pthread_once_t cups_globals_key_once = PTHREAD_ONCE_INIT;
/* One-time initialization object */
#endif /* HAVE_PTHREAD_H */
-#if defined(HAVE_PTHREAD_H) || defined(WIN32)
+#if defined(HAVE_PTHREAD_H) || defined(_WIN32)
static _cups_mutex_t cups_global_mutex = _CUPS_MUTEX_INITIALIZER;
/* Global critical section */
-#endif /* HAVE_PTHREAD_H || WIN32 */
+#endif /* HAVE_PTHREAD_H || _WIN32 */
/*
* Local functions...
*/
+#ifdef _WIN32
+static void cups_fix_path(char *path);
+#endif /* _WIN32 */
static _cups_globals_t *cups_globals_alloc(void);
-#if defined(HAVE_PTHREAD_H) || defined(WIN32)
+#if defined(HAVE_PTHREAD_H) || defined(_WIN32)
static void cups_globals_free(_cups_globals_t *g);
-#endif /* HAVE_PTHREAD_H || WIN32 */
+#endif /* HAVE_PTHREAD_H || _WIN32 */
#ifdef HAVE_PTHREAD_H
static void cups_globals_init(void);
#endif /* HAVE_PTHREAD_H */
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&cups_global_mutex);
-#elif defined(WIN32)
+#elif defined(_WIN32)
EnterCriticalSection(&cups_global_mutex.m_criticalSection);
#endif /* HAVE_PTHREAD_H */
}
{
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&cups_global_mutex);
-#elif defined(WIN32)
+#elif defined(_WIN32)
LeaveCriticalSection(&cups_global_mutex.m_criticalSection);
#endif /* HAVE_PTHREAD_H */
}
-#ifdef WIN32
+#ifdef _WIN32
/*
* 'DllMain()' - Main entry for library.
*/
return (TRUE);
}
-#endif /* WIN32 */
+#endif /* _WIN32 */
/*
{
_cups_globals_t *cg = malloc(sizeof(_cups_globals_t));
/* Pointer to global data */
-#ifdef WIN32
+#ifdef _WIN32
HKEY key; /* Registry key */
DWORD size; /* Size of string */
- static char installdir[1024], /* Install directory */
- confdir[1024], /* Server root directory */
- localedir[1024]; /* Locale directory */
-#endif /* WIN32 */
+ static char installdir[1024] = "", /* Install directory */
+ confdir[1024] = "", /* Server root directory */
+ localedir[1024] = ""; /* Locale directory */
+#endif /* _WIN32 */
if (!cg)
*/
memset(cg, 0, sizeof(_cups_globals_t));
- cg->encryption = (http_encryption_t)-1;
- cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
- cg->any_root = 1;
- cg->expired_certs = 1;
- cg->expired_root = 1;
-
+ cg->encryption = (http_encryption_t)-1;
+ cg->password_cb = (cups_password_cb2_t)_cupsGetPassword;
+ cg->trust_first = -1;
+ cg->any_root = -1;
+ cg->expired_certs = -1;
+ cg->validate_certs = -1;
+
+#ifdef DEBUG
/*
- * Then set directories as appropriate...
+ * Friendly thread ID for debugging...
*/
-#ifdef WIN32
+ cg->thread_id = ++ cups_global_index;
+#endif /* DEBUG */
+
/*
- * Open the registry...
+ * Then set directories as appropriate...
*/
- strcpy(installdir, "C:/Program Files/cups.org");
-
- if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ,
- &key))
+#ifdef _WIN32
+ if (!installdir[0])
{
/*
- * Grab the installation directory...
+ * Open the registry...
*/
- size = sizeof(installdir);
- RegQueryValueEx(key, "installdir", NULL, NULL, installdir, &size);
- RegCloseKey(key);
+ strlcpy(installdir, "C:/Program Files/cups.org", sizeof(installdir));
+
+ if (!RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\cups.org", 0, KEY_READ, &key))
+ {
+ /*
+ * Grab the installation directory...
+ */
+
+ char *ptr; /* Pointer into installdir */
+
+ size = sizeof(installdir);
+ RegQueryValueExA(key, "installdir", NULL, NULL, installdir, &size);
+ RegCloseKey(key);
+
+ for (ptr = installdir; *ptr;)
+ {
+ if (*ptr == '\\')
+ {
+ if (ptr[1])
+ *ptr++ = '/';
+ else
+ *ptr = '\0'; /* Strip trailing \ */
+ }
+ else if (*ptr == '/' && !ptr[1])
+ *ptr = '\0'; /* Strip trailing / */
+ else
+ ptr ++;
+ }
+ }
+
+ snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
+ snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
}
- snprintf(confdir, sizeof(confdir), "%s/conf", installdir);
- snprintf(localedir, sizeof(localedir), "%s/locale", installdir);
-
if ((cg->cups_datadir = getenv("CUPS_DATADIR")) == NULL)
cg->cups_datadir = installdir;
if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
cg->localedir = localedir;
+ cg->home = getenv("HOME");
+
#else
# ifdef HAVE_GETEUID
if ((geteuid() != getuid() && getuid()) || getegid() != getgid())
if ((cg->localedir = getenv("LOCALEDIR")) == NULL)
cg->localedir = CUPS_LOCALEDIR;
+
+ cg->home = getenv("HOME");
+
+# ifdef __APPLE__ /* Sandboxing now exposes the container as the home directory */
+ if (cg->home && strstr(cg->home, "/Library/Containers/"))
+ cg->home = NULL;
+# endif /* !__APPLE__ */
+ }
+
+ if (!cg->home)
+ {
+ struct passwd *pw; /* User info */
+
+ if ((pw = getpwuid(getuid())) != NULL)
+ cg->home = _cupsStrAlloc(pw->pw_dir);
}
-#endif /* WIN32 */
+#endif /* _WIN32 */
return (cg);
}
* 'cups_globals_free()' - Free global data.
*/
-#if defined(HAVE_PTHREAD_H) || defined(WIN32)
+#if defined(HAVE_PTHREAD_H) || defined(_WIN32)
static void
cups_globals_free(_cups_globals_t *cg) /* I - Pointer to global data */
{
- _ipp_buffer_t *buffer, /* Current IPP read/write buffer */
+ _cups_buffer_t *buffer, /* Current read/write buffer */
*next; /* Next buffer */
if (cg->last_status_message)
_cupsStrFree(cg->last_status_message);
- for (buffer = cg->ipp_buffers; buffer; buffer = next)
+ for (buffer = cg->cups_buffers; buffer; buffer = next)
{
next = buffer->next;
free(buffer);
httpClose(cg->http);
+#ifdef HAVE_SSL
_httpFreeCredentials(cg->tls_credentials);
+#endif /* HAVE_SSL */
cupsFileClose(cg->stdio_files[0]);
cupsFileClose(cg->stdio_files[1]);
cupsFreeOptions(cg->cupsd_num_settings, cg->cupsd_settings);
+ if (cg->raster_error.start)
+ free(cg->raster_error.start);
+
free(cg);
}
-#endif /* HAVE_PTHREAD_H || WIN32 */
+#endif /* HAVE_PTHREAD_H || _WIN32 */
#ifdef HAVE_PTHREAD_H
pthread_key_create(&cups_globals_key, (void (*)(void *))cups_globals_free);
}
#endif /* HAVE_PTHREAD_H */
-
-
-/*
- * End of "$Id: globals.c 10436 2012-04-23 21:52:02Z mike $".
- */