Changeset 395e5d4 in rtems


Ignore:
Timestamp:
Nov 14, 2014, 2:33:57 PM (5 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
ffa71f1
Parents:
acf9a8d
git-author:
Sebastian Huber <sebastian.huber@…> (11/14/14 14:33:57)
git-committer:
Sebastian Huber <sebastian.huber@…> (11/20/14 09:30:22)
Message:

libcsupport: Use POSIX key for getgrent()

Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libcsupport/Makefile.am

    racf9a8d r395e5d4  
    111111PASSWORD_GROUP_C_FILES = src/pwdgrp.c
    112112PASSWORD_GROUP_C_FILES += src/getgrent.c
     113PASSWORD_GROUP_C_FILES += src/getgrnam.c
    113114PASSWORD_GROUP_C_FILES += src/getpwent.c
    114115
  • cpukit/libcsupport/src/getgrent.c

    racf9a8d r395e5d4  
    2525#include "pwdgrp.h"
    2626
    27 /*
    28  * Static, thread-unsafe, buffers
    29  */
    30 static FILE *group_fp;
    31 static char grbuf[200];
    32 static struct group grent;
     27#include <stdlib.h>
     28#include <pthread.h>
    3329
    34 struct group *getgrnam(
    35   const char *name
    36 )
     30typedef struct {
     31  FILE *fp;
     32  char buf[256];
     33  struct group grp;
     34} grp_context;
     35
     36static pthread_once_t grp_once = PTHREAD_ONCE_INIT;
     37
     38static pthread_key_t grp_key;
     39
     40static void grp_init(void)
    3741{
    38   struct group *p;
    39 
    40   if(getgrnam_r(name, &grent, grbuf, sizeof grbuf, &p))
    41     return NULL;
    42   return p;
     42  pthread_key_create(&grp_key, free);
    4343}
    4444
    45 struct group *getgrgid(
    46   gid_t gid
    47 )
     45static grp_context *grp_get_context(void)
    4846{
    49   struct group *p;
     47  pthread_once(&grp_once, grp_init);
    5048
    51   if(getgrgid_r(gid, &grent, grbuf, sizeof grbuf, &p))
    52     return NULL;
    53   return p;
     49  return pthread_getspecific(grp_key);
    5450}
    5551
    5652struct group *getgrent(void)
    5753{
    58   if (group_fp == NULL)
     54  grp_context *ctx = grp_get_context();
     55
     56  if (ctx == NULL)
    5957    return NULL;
    60   if (!_libcsupport_scangr(group_fp, &grent, grbuf, sizeof grbuf))
     58
     59  if (ctx->fp == NULL)
    6160    return NULL;
    62   return &grent;
     61
     62  if (!_libcsupport_scangr(ctx->fp, &ctx->grp, ctx->buf, sizeof(ctx->buf)))
     63    return NULL;
     64
     65  return &ctx->grp;
    6366}
    6467
    6568void setgrent(void)
    6669{
     70  grp_context *ctx = grp_get_context();
     71
     72  if (ctx == NULL) {
     73    int eno;
     74
     75    ctx = calloc(1, sizeof(*ctx));
     76    if (ctx == NULL)
     77      return;
     78
     79    eno = pthread_setspecific(grp_key, ctx);
     80    if (eno != 0) {
     81      free(ctx);
     82
     83      return;
     84    }
     85  }
     86
    6787  _libcsupport_pwdgrp_init();
    6888
    69   if (group_fp != NULL)
    70     fclose(group_fp);
    71   group_fp = fopen("/etc/group", "r");
     89  if (ctx->fp != NULL)
     90    fclose(ctx->fp);
     91
     92  ctx->fp = fopen("/etc/group", "r");
    7293}
    7394
    7495void endgrent(void)
    7596{
    76   if (group_fp != NULL)
    77     fclose(group_fp);
     97  grp_context *ctx = grp_get_context();
     98
     99  if (ctx == NULL)
     100    return;
     101
     102  if (ctx->fp != NULL) {
     103    fclose(ctx->fp);
     104  }
     105
     106  free(ctx);
     107  pthread_setspecific(grp_key, NULL);
    78108}
  • testsuites/psxtests/psxpasswd01/init.c

    racf9a8d r395e5d4  
    185185
    186186#define CONFIGURE_MAXIMUM_TASKS 1
     187#define CONFIGURE_MAXIMUM_POSIX_KEYS 1
     188#define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 1
    187189#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
    188190
  • testsuites/psxtests/psxpasswd02/init.c

    racf9a8d r395e5d4  
    227227
    228228#define CONFIGURE_MAXIMUM_TASKS 1
     229#define CONFIGURE_MAXIMUM_POSIX_KEYS 1
     230#define CONFIGURE_MAXIMUM_POSIX_KEY_VALUE_PAIRS 1
    229231#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
    230232
Note: See TracChangeset for help on using the changeset viewer.