mirror of
https://gitlab.rtems.org/rtems/rtos/rtems.git
synced 2026-09-27 16:05:39 +08:00
libcsupport: Use POSIX key for getgrent()
This commit is contained in:
@@ -110,6 +110,7 @@ MALLOC_C_FILES = src/malloc_initialize.c src/calloc.c src/malloc.c \
|
|||||||
|
|
||||||
PASSWORD_GROUP_C_FILES = src/pwdgrp.c
|
PASSWORD_GROUP_C_FILES = src/pwdgrp.c
|
||||||
PASSWORD_GROUP_C_FILES += src/getgrent.c
|
PASSWORD_GROUP_C_FILES += src/getgrent.c
|
||||||
|
PASSWORD_GROUP_C_FILES += src/getgrnam.c
|
||||||
PASSWORD_GROUP_C_FILES += src/getpwent.c
|
PASSWORD_GROUP_C_FILES += src/getpwent.c
|
||||||
|
|
||||||
TERMINAL_IDENTIFICATION_C_FILES = src/ctermid.c
|
TERMINAL_IDENTIFICATION_C_FILES = src/ctermid.c
|
||||||
|
|||||||
@@ -24,55 +24,85 @@
|
|||||||
|
|
||||||
#include "pwdgrp.h"
|
#include "pwdgrp.h"
|
||||||
|
|
||||||
/*
|
#include <stdlib.h>
|
||||||
* Static, thread-unsafe, buffers
|
#include <pthread.h>
|
||||||
*/
|
|
||||||
static FILE *group_fp;
|
|
||||||
static char grbuf[200];
|
|
||||||
static struct group grent;
|
|
||||||
|
|
||||||
struct group *getgrnam(
|
typedef struct {
|
||||||
const char *name
|
FILE *fp;
|
||||||
)
|
char buf[256];
|
||||||
|
struct group grp;
|
||||||
|
} grp_context;
|
||||||
|
|
||||||
|
static pthread_once_t grp_once = PTHREAD_ONCE_INIT;
|
||||||
|
|
||||||
|
static pthread_key_t grp_key;
|
||||||
|
|
||||||
|
static void grp_init(void)
|
||||||
{
|
{
|
||||||
struct group *p;
|
pthread_key_create(&grp_key, free);
|
||||||
|
|
||||||
if(getgrnam_r(name, &grent, grbuf, sizeof grbuf, &p))
|
|
||||||
return NULL;
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct group *getgrgid(
|
static grp_context *grp_get_context(void)
|
||||||
gid_t gid
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
struct group *p;
|
pthread_once(&grp_once, grp_init);
|
||||||
|
|
||||||
if(getgrgid_r(gid, &grent, grbuf, sizeof grbuf, &p))
|
return pthread_getspecific(grp_key);
|
||||||
return NULL;
|
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct group *getgrent(void)
|
struct group *getgrent(void)
|
||||||
{
|
{
|
||||||
if (group_fp == NULL)
|
grp_context *ctx = grp_get_context();
|
||||||
|
|
||||||
|
if (ctx == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
if (!_libcsupport_scangr(group_fp, &grent, grbuf, sizeof grbuf))
|
|
||||||
|
if (ctx->fp == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
return &grent;
|
|
||||||
|
if (!_libcsupport_scangr(ctx->fp, &ctx->grp, ctx->buf, sizeof(ctx->buf)))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return &ctx->grp;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setgrent(void)
|
void setgrent(void)
|
||||||
{
|
{
|
||||||
|
grp_context *ctx = grp_get_context();
|
||||||
|
|
||||||
|
if (ctx == NULL) {
|
||||||
|
int eno;
|
||||||
|
|
||||||
|
ctx = calloc(1, sizeof(*ctx));
|
||||||
|
if (ctx == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
eno = pthread_setspecific(grp_key, ctx);
|
||||||
|
if (eno != 0) {
|
||||||
|
free(ctx);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_libcsupport_pwdgrp_init();
|
_libcsupport_pwdgrp_init();
|
||||||
|
|
||||||
if (group_fp != NULL)
|
if (ctx->fp != NULL)
|
||||||
fclose(group_fp);
|
fclose(ctx->fp);
|
||||||
group_fp = fopen("/etc/group", "r");
|
|
||||||
|
ctx->fp = fopen("/etc/group", "r");
|
||||||
}
|
}
|
||||||
|
|
||||||
void endgrent(void)
|
void endgrent(void)
|
||||||
{
|
{
|
||||||
if (group_fp != NULL)
|
grp_context *ctx = grp_get_context();
|
||||||
fclose(group_fp);
|
|
||||||
|
if (ctx == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (ctx->fp != NULL) {
|
||||||
|
fclose(ctx->fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(ctx);
|
||||||
|
pthread_setspecific(grp_key, NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* @file
|
||||||
|
*
|
||||||
|
* @brief User Database Access Routines
|
||||||
|
* @ingroup libcsupport
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1999-2009 Ralf Corsepius <corsepiu@faw.uni-ulm.de>
|
||||||
|
* Copyright (c) 1999-2013 Joel Sherrill <joel.sherrill@OARcorp.com>
|
||||||
|
* Copyright (c) 2000-2001 Fernando Ruiz Casas <fernando.ruiz@ctv.es>
|
||||||
|
* Copyright (c) 2002 Eric Norum <eric.norum@usask.ca>
|
||||||
|
* Copyright (c) 2003 Till Straumann <strauman@slac.stanford.edu>
|
||||||
|
* Copyright (c) 2012 Alex Ivanov <alexivanov97@gmail.com>
|
||||||
|
*
|
||||||
|
* The license and distribution terms for this file may be
|
||||||
|
* found in the file LICENSE in this distribution or at
|
||||||
|
* http://www.rtems.org/license/LICENSE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if HAVE_CONFIG_H
|
||||||
|
#include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "pwdgrp.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Static, thread-unsafe, buffers
|
||||||
|
*/
|
||||||
|
static char grbuf[200];
|
||||||
|
static struct group grent;
|
||||||
|
|
||||||
|
struct group *getgrnam(
|
||||||
|
const char *name
|
||||||
|
)
|
||||||
|
{
|
||||||
|
struct group *p;
|
||||||
|
|
||||||
|
if(getgrnam_r(name, &grent, grbuf, sizeof grbuf, &p))
|
||||||
|
return NULL;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct group *getgrgid(
|
||||||
|
gid_t gid
|
||||||
|
)
|
||||||
|
{
|
||||||
|
struct group *p;
|
||||||
|
|
||||||
|
if(getgrgid_r(gid, &grent, grbuf, sizeof grbuf, &p))
|
||||||
|
return NULL;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
@@ -184,6 +184,8 @@ rtems_task Init(
|
|||||||
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 6
|
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 6
|
||||||
|
|
||||||
#define CONFIGURE_MAXIMUM_TASKS 1
|
#define CONFIGURE_MAXIMUM_TASKS 1
|
||||||
|
#define CONFIGURE_MAXIMUM_POSIX_KEYS 1
|
||||||
|
#define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 1
|
||||||
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
|
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
|
||||||
|
|
||||||
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
|
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
|
||||||
|
|||||||
@@ -226,6 +226,8 @@ rtems_task Init(
|
|||||||
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 6
|
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 6
|
||||||
|
|
||||||
#define CONFIGURE_MAXIMUM_TASKS 1
|
#define CONFIGURE_MAXIMUM_TASKS 1
|
||||||
|
#define CONFIGURE_MAXIMUM_POSIX_KEYS 1
|
||||||
|
#define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 1
|
||||||
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
|
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
|
||||||
|
|
||||||
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
|
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
|
||||||
|
|||||||
Reference in New Issue
Block a user