newlib/winsup/cygwin/registry.cc

253 lines
5.7 KiB
C++
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* registry.cc: registry interface
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
2000-02-17 20:38:33 +01:00
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include "shared_info.h"
#include "registry.h"
#include "security.h"
#include <cygwin/version.h>
2000-02-17 20:38:33 +01:00
static char NO_COPY cygnus_class[] = "cygnus";
2000-02-17 20:38:33 +01:00
reg_key::reg_key (HKEY top, REGSAM access, ...)
{
va_list av;
va_start (av, access);
build_reg (top, access, av);
va_end (av);
}
reg_key::reg_key (REGSAM access, ...)
{
va_list av;
new (this) reg_key (HKEY_CURRENT_USER, access, "SOFTWARE",
CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
CYGWIN_INFO_CYGWIN_REGISTRY_NAME, NULL);
HKEY top = key;
va_start (av, access);
build_reg (top, KEY_READ, av);
va_end (av);
if (top != key)
RegCloseKey (top);
}
reg_key::reg_key (REGSAM access)
{
new (this) reg_key (HKEY_CURRENT_USER, access, "SOFTWARE",
CYGWIN_INFO_CYGNUS_REGISTRY_NAME,
CYGWIN_INFO_CYGWIN_REGISTRY_NAME,
CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME, NULL);
}
void
reg_key::build_reg (HKEY top, REGSAM access, va_list av)
{
char *name;
HKEY r = top;
key_is_invalid = 0;
2000-02-17 20:38:33 +01:00
/* FIXME: Most of the time a valid mount area should exist. Perhaps
we should just try an open of the correct key first and only resort
to this method in the unlikely situation that it's the first time
the current mount areas are being used. */
while ((name = va_arg (av, char *)) != NULL)
{
DWORD disp;
int res = RegCreateKeyExA (r,
name,
0,
cygnus_class,
REG_OPTION_NON_VOLATILE,
access,
&sec_none_nih,
&key,
&disp);
if (r != top)
RegCloseKey (r);
r = key;
if (res != ERROR_SUCCESS)
{
key_is_invalid = res;
2000-02-17 20:38:33 +01:00
debug_printf ("failed to create key %s in the registry", name);
break;
}
/* If we're considering the mounts key, check if it had to
be created and set had_to_create appropriately. */
if (strcmp (name, CYGWIN_INFO_CYGWIN_MOUNT_REGISTRY_NAME) == 0)
if (disp == REG_CREATED_NEW_KEY)
Throughout, change 'cygwin_shared.mount' to 'mount_table'. * child_info.h (child_info): Move shared_h, console_h to cygheap. Add mount_h. * cygheap.h (init_cygheap): Add shared_h, console_h. * cygheap.cc (init_cheap): Initialize heap at a fixed location after the shared memory regions. Initialize cygheap->user name here. * dcrt0.cc (dll_crt0_1): Call getpagesize () to initialize constants. Remove cygheap_init since it is done in shared_init now. (_dll_crt0): Initialize mount_h, remove shared_h and console_h initialization. * fhandler_console.cc (console_shared_h): Eliminate. (get_tty_stuff): Use cygheap->console_h rather than console_shared_h. * heap.cc (heap_init): Use page size constant calculated earlier in initialization. * shared.cc: Eliminate cygwin_shared_h. Add cygwin_mount_h. (mount_table_init): New function for initializing a user mount table. (open_shared_file_map): Use constant for shared memory region. Initialize cygheap and mount table here. (open_shared): Improve debugging output. (shared_info::initialize): Eliminate call to mount.init. (shared_terminate): Use cygheap->shared_h. Close cygwin_mount_h. (open_shared_file_map): Eliminate. * shared_info.h (mount_info): Add a version field. (shared_align_past): New macro for calculating location for shared memory regions. * sigproc.cc (init_child_info): Eliminate shared_h, console_h. * spawn.cc (spawn_guts): Pass on cygwin_mount_h iff not a different user. * syscalls.cc (system_info): New global holding system memory defaults. (getpagesize): Use system_info. * uinfo.cc (internal_getlogin): Only fill in user name if nonexistent. * winsup.h: Declare system_info. * passwd.cc (read_etc_passwd): Use cygheap->user.name () rather than retrieving the name again.
2001-01-28 06:51:15 +01:00
mount_table->had_to_create_mount_areas++;
2000-02-17 20:38:33 +01:00
}
}
/* Given the current registry key, return the specific int value
requested. Return def on failure. */
int
reg_key::get_int (const char *name, int def)
{
DWORD type;
DWORD dst;
DWORD size = sizeof (dst);
if (key_is_invalid)
return def;
LONG res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) &dst,
&size);
2000-02-17 20:38:33 +01:00
if (type != REG_DWORD || res != ERROR_SUCCESS)
return def;
return dst;
}
/* Given the current registry key, set a specific int value. */
int
reg_key::set_int (const char *name, int val)
{
DWORD value = val;
if (key_is_invalid)
return key_is_invalid;
2000-02-17 20:38:33 +01:00
return (int) RegSetValueExA (key, name, 0, REG_DWORD,
(unsigned char *) &value, sizeof (value));
}
/* Given the current registry key, return the specific string value
requested. Return zero on success, non-zero on failure. */
int
reg_key::get_string (const char *name, char *dst, size_t max, const char * def)
{
DWORD size = max;
DWORD type;
LONG res;
if (key_is_invalid)
res = key_is_invalid;
else
res = RegQueryValueExA (key, name, 0, &type, (unsigned char *) dst, &size);
2000-02-17 20:38:33 +01:00
if ((def != 0) && ((type != REG_SZ) || (res != ERROR_SUCCESS)))
strcpy (dst, def);
2000-02-17 20:38:33 +01:00
return (int) res;
}
/* Given the current registry key, set a specific string value. */
int
reg_key::set_string (const char *name, const char *src)
{
if (key_is_invalid)
return key_is_invalid;
2000-02-17 20:38:33 +01:00
return (int) RegSetValueExA (key, name, 0, REG_SZ, (unsigned char*) src,
strlen (src) + 1);
}
/* Return the handle to key. */
HKEY
reg_key::get_key ()
{
return key;
}
/* Delete subkey of current key. Returns the error code from the
RegDeleteKeyA invocation. */
int
reg_key::kill (const char *name)
{
if (key_is_invalid)
return key_is_invalid;
2000-02-17 20:38:33 +01:00
return RegDeleteKeyA (key, name);
}
/* Delete the value specified by name of current key. Returns the error code
from the RegDeleteValueA invocation. */
int
reg_key::killvalue (const char *name)
{
if (key_is_invalid)
return key_is_invalid;
return RegDeleteValueA (key, name);
}
2000-02-17 20:38:33 +01:00
reg_key::~reg_key ()
{
if (!key_is_invalid)
2000-02-17 20:38:33 +01:00
RegCloseKey (key);
key_is_invalid = 1;
2000-02-17 20:38:33 +01:00
}
char *
get_registry_hive_path (const char *name, char *path)
{
char key[256];
HKEY hkey;
if (!name || !path)
return NULL;
__small_sprintf (key, "SOFTWARE\\Microsoft\\Windows%s\\CurrentVersion\\ProfileList\\",
wincap.is_winnt ()?" NT":"");
strcat (key, name);
if (!RegOpenKeyExA (HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hkey))
{
char buf[256];
DWORD type, siz;
path[0] = '\0';
if (!RegQueryValueExA (hkey, "ProfileImagePath", 0, &type,
(BYTE *)buf, (siz = sizeof (buf), &siz)))
ExpandEnvironmentStringsA (buf, path, CYG_MAX_PATH + 1);
RegCloseKey (hkey);
if (path[0])
return path;
}
debug_printf ("HKLM\\%s not found", key);
return NULL;
}
void
load_registry_hive (const char * name)
{
char path[CYG_MAX_PATH + 1];
HKEY hkey;
LONG ret;
if (!name)
return;
/* Check if user hive is already loaded. */
if (!RegOpenKeyExA (HKEY_USERS, name, 0, KEY_READ, &hkey))
{
debug_printf ("User registry hive for %s already exists", name);
RegCloseKey (hkey);
return;
}
/* This is only called while deimpersonated */
set_process_privilege (SE_RESTORE_NAME);
if (get_registry_hive_path (name, path))
{
if (wincap.is_winnt ())
strcat (path, "\\NTUSER.DAT");
else
strcat (path, "\\USER.DAT");
if ((ret = RegLoadKeyA (HKEY_USERS, name, path)) != ERROR_SUCCESS)
debug_printf ("Loading user registry hive for %s failed: %d", name, ret);
}
}