newlib/winsup/cygwin/grp.cc
Christopher Faylor 166b2571ce * Makefile.in: Remove some obsolete stuff.
* dcrt0.cc (dll_crt0_1): Call signal_fixup_after_exec where appropriate.  Set
myself->uid from parent version.  Just use ThreadItem Init method.  Close or
store hexec_proc as appropriate.
(_dll_crt0): Store user_data->forkee here so that proper tests can be made
subsequently.
(do_exit): Remove hExeced stuff.
* environ.cc (environ_init): Accept environ count as well as environ pointer.
* environ.h: Reflect above change.
* pinfo.cc (pinfo_init): Ditto.  Accept environ count.
(fixup_in_spawned_child): Remove.
* spawn.cc (spawn_guts): Move signal code to dll_crt0_1.  Don't suspend execing
process since it is no longer necessary.  Store envc.
* exceptions.cc (signal_fixup_after_exec): New function.
(call_handler): Remove hExeced test.
* child_info.h (cygheap_exec_info): Store envc as well as envp.
(child_info_spawn): Store hexec_proc so that it can be closed in child.
* path.cc (normalize_posix_path): Avoid intermediate use of temporary cwd buf.
(normalize_win32_path): Ditto.
(cwdstuff::get_initial): Always set lock.
* sigproc.h: Remove hExeced.
* strace.cc (strace::vsprntf): Modify to accomodate for lack of hExeced.
* thread.cc (MTinterface::Init): Merge Init1 and ClearReent into this method.
(MTinterface::Init1): Eliminate.
(MTinterface::ClearReent): Eliminate.
* thread.h: Reflect above changes.
* include/sys/strace.h (strace): Make microseconds() public.  Make various
functions 'regparm', throughout.
* pinfo.h (_pinfo): Inline simple signal manipulation functions.  Requires
inclusion of thread.h which was removed from .cc files, where appropriate.
throughout.
* pinfo.cc: Eliminate signal manipulation functions.
(_pinfo::exit): Calculate total rusage for exiting process here.
* cygheap.cc (size2bucket): Eliminate.
(init_buckets): Ditto.
(_cmalloc): Calculate size and bits in a loop rather than going through a
function call.
(_crealloc): Use stored array index to calculate allocated size.
* spawn.cc (spawn_guts): Use _pinfo exit method to exit, calculating cpu usage.
2000-10-16 23:55:58 +00:00

292 lines
6.3 KiB
C++

/* grp.cc
Copyright 1996, 1997, 1998, 2000 Cygnus Solutions.
Original stubs by Jason Molenda of Cygnus Support, crash@cygnus.com
First implementation by Gunther Ebert, gunther.ebert@ixos-leipzig.de
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 <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "cygerrno.h"
/* Read /etc/group only once for better performance. This is done
on the first call that needs information from it. */
#define MAX_DOMAIN_NAME 100
static NO_COPY const char *etc_group = "/etc/group";
static struct group *group_buf = NULL; /* group contents in memory */
static int curr_lines = 0;
static int max_lines = 0;
/* Position in the group cache */
#ifdef _MT_SAFE
#define grp_pos _reent_winsup()->_grp_pos
#else
static int grp_pos = 0;
#endif
/* Set to 1 when /etc/group has been read in by read_etc_group (). */
/* Functions in this file need to check the value of group_in_memory_p
and read in the group file if it isn't set. */
/* FIXME: This should be static but this is called in uinfo_init outside
this file */
int group_in_memory_p = 0;
static int
parse_grp (struct group &grp, const char *line)
{
int len = strlen(line);
char *newline = (char *) malloc (len + 1);
(void) memcpy (newline, line, len + 1);
if (newline[--len] == '\n')
newline[len] = '\0';
char *dp = strchr (newline, ':');
if (!dp)
return 0;
*dp++ = '\0';
grp.gr_name = newline;
grp.gr_passwd = dp;
dp = strchr (grp.gr_passwd, ':');
if (dp)
{
*dp++ = '\0';
if (!strlen (grp.gr_passwd))
grp.gr_passwd = NULL;
grp.gr_gid = strtol (dp, NULL, 10);
dp = strchr (dp, ':');
if (dp)
{
if (*++dp)
{
int i = 0;
char *cp;
for (cp = dp; (cp = strchr (cp, ',')) != NULL; ++cp)
++i;
char **namearray = (char **) calloc (i + 2, sizeof (char *));
if (namearray)
{
i = 0;
for (cp = dp; (cp = strchr (dp, ',')) != NULL; dp = cp + 1)
{
*cp = '\0';
namearray[i++] = dp;
}
namearray[i++] = dp;
namearray[i] = NULL;
}
grp.gr_mem = namearray;
}
else
grp.gr_mem = (char **) calloc (1, sizeof (char *));
return 1;
}
}
return 0;
}
/* Read one line from /etc/group into the group cache */
static void
add_grp_line (const char *line)
{
if (curr_lines == max_lines)
{
max_lines += 10;
group_buf = (struct group *) realloc (group_buf, max_lines * sizeof (struct group));
}
if (parse_grp (group_buf[curr_lines], line))
curr_lines++;
}
extern PSID get_admin_sid ();
/* Cygwin internal */
/* Read in /etc/group and save contents in the group cache */
/* This sets group_in_memory_p to 1 so functions in this file can
tell that /etc/group has been read in */
/* FIXME: should be static but this is called in uinfo_init outside this
file */
void
read_etc_group ()
{
extern int group_sem;
char linebuf [ 200 ];
char group_name [ MAX_USER_NAME ];
DWORD group_name_len = MAX_USER_NAME;
strncpy (group_name, "Administrators", sizeof (group_name));
++group_sem;
FILE *f = fopen (etc_group, "rt");
--group_sem;
if (f)
{
while (fgets (linebuf, sizeof (linebuf), f) != NULL)
{
if (strlen (linebuf))
add_grp_line (linebuf);
}
fclose (f);
}
else /* /etc/group doesn't exist -- create default one in memory */
{
char domain_name [ MAX_DOMAIN_NAME ];
DWORD domain_name_len = MAX_DOMAIN_NAME;
SID_NAME_USE acType;
debug_printf ("Emulating /etc/group");
if (! LookupAccountSidA (NULL ,
get_admin_sid () ,
group_name,
&group_name_len,
domain_name,
&domain_name_len,
&acType))
{
strcpy (group_name, "unknown");
debug_printf ("Failed to get local admins group name. %E");
}
snprintf (linebuf, sizeof (linebuf), "%s::%u:\n", group_name, DEFAULT_GID);
add_grp_line (linebuf);
}
group_in_memory_p = 1;
}
extern "C"
struct group *
getgrgid (gid_t gid)
{
struct group * default_grp = NULL;
if (!group_in_memory_p)
read_etc_group();
for (int i = 0; i < curr_lines; i++)
{
if (group_buf[i].gr_gid == DEFAULT_GID)
default_grp = group_buf + i;
if (group_buf[i].gr_gid == gid)
return group_buf + i;
}
return default_grp;
}
extern "C"
struct group *
getgrnam (const char *name)
{
if (!group_in_memory_p)
read_etc_group();
for (int i = 0; i < curr_lines; i++)
if (strcasematch (group_buf[i].gr_name, name))
return group_buf + i;
/* Didn't find requested group */
return NULL;
}
extern "C"
void
endgrent()
{
grp_pos = 0;
}
extern "C"
struct group *
getgrent()
{
if (!group_in_memory_p)
read_etc_group();
if (grp_pos < curr_lines)
return group_buf + grp_pos++;
return NULL;
}
extern "C"
void
setgrent ()
{
grp_pos = 0;
}
int
getgroups (int gidsetsize, gid_t *grouplist, gid_t gid, const char *username)
{
if (!group_in_memory_p)
read_etc_group();
int cnt = 0;
for (int i = 0; i < curr_lines; ++i)
if (gid == group_buf[i].gr_gid)
{
if (cnt < gidsetsize)
grouplist[cnt] = group_buf[i].gr_gid;
++cnt;
if (gidsetsize && cnt > gidsetsize)
goto error;
}
else if (group_buf[i].gr_mem)
for (int gi = 0; group_buf[i].gr_mem[gi]; ++gi)
if (strcasematch (username, group_buf[i].gr_mem[gi]))
{
if (cnt < gidsetsize)
grouplist[cnt] = group_buf[i].gr_gid;
++cnt;
if (gidsetsize && cnt > gidsetsize)
goto error;
}
return cnt;
error:
set_errno (EINVAL);
return -1;
}
extern "C"
int
getgroups (int gidsetsize, gid_t *grouplist)
{
#if 0
if (gidsetsize <= 0)
return 0;
grouplist[0] = myself->gid;
return 1;
#else
return getgroups (gidsetsize, grouplist, myself->gid, myself->username);
#endif
}
extern "C"
int
initgroups (const char *, gid_t)
{
return 0;
}