Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:15

0001 /**
0002  *  @file
0003  *
0004  *  @brief User Database Access Routines
0005  *  @ingroup libcsupport
0006  */
0007 
0008 /*
0009  *  Copyright (c) 1999-2009 Ralf Corsepius <corsepiu@faw.uni-ulm.de>
0010  *  Copyright (c) 1999-2013 Joel Sherrill <joel.sherrill@OARcorp.com>
0011  *  Copyright (c) 2000-2001 Fernando Ruiz Casas <fruizcasas@gmail.com>
0012  *  Copyright (c) 2002 Eric Norum <eric@norum.ca>
0013  *  Copyright (c) 2003 Till Straumann <strauman@slac.stanford.edu>
0014  *  Copyright (c) 2012 Alex Ivanov <alexivanov97@gmail.com>
0015  *
0016  *  The license and distribution terms for this file may be
0017  *  found in the file LICENSE in this distribution or at
0018  *  http://www.rtems.org/license/LICENSE.
0019  */
0020 
0021 #ifdef HAVE_CONFIG_H
0022 #include "config.h"
0023 #endif
0024 
0025 #include "pwdgrp.h"
0026 
0027 #include <stdlib.h>
0028 #include <pthread.h>
0029 #include <rtems/score/assert.h>
0030 
0031 typedef struct {
0032   FILE *fp;
0033   char buf[256];
0034   struct group grp;
0035 } grp_context;
0036 
0037 static pthread_once_t grp_once = PTHREAD_ONCE_INIT;
0038 
0039 static pthread_key_t grp_key;
0040 
0041 static void grp_init(void)
0042 {
0043   pthread_key_create(&grp_key, free);
0044 }
0045 
0046 static grp_context *grp_get_context(void)
0047 {
0048   pthread_once(&grp_once, grp_init);
0049 
0050   return pthread_getspecific(grp_key);
0051 }
0052 
0053 struct group *getgrent(void)
0054 {
0055   grp_context *ctx = grp_get_context();
0056 
0057   if (ctx == NULL)
0058     return NULL;
0059 
0060   if (ctx->fp == NULL)
0061     return NULL;
0062 
0063   if (!_libcsupport_scangr(ctx->fp, &ctx->grp, ctx->buf, sizeof(ctx->buf)))
0064     return NULL;
0065 
0066   return &ctx->grp;
0067 }
0068 
0069 void setgrent(void)
0070 {
0071   grp_context *ctx = grp_get_context();
0072 
0073   if (ctx == NULL) {
0074     int eno;
0075 
0076     ctx = calloc(1, sizeof(*ctx));
0077     if (ctx == NULL)
0078       return;
0079 
0080     eno = pthread_setspecific(grp_key, ctx);
0081     if (eno != 0) {
0082       free(ctx);
0083 
0084       return;
0085     }
0086   }
0087 
0088   _libcsupport_pwdgrp_init();
0089 
0090   if (ctx->fp != NULL)
0091     fclose(ctx->fp);
0092 
0093   ctx->fp = fopen("/etc/group", "r");
0094 }
0095 
0096 void endgrent(void)
0097 {
0098   grp_context *ctx = grp_get_context();
0099   int          sc;
0100 
0101   if (ctx == NULL)
0102     return;
0103 
0104   if (ctx->fp != NULL) {
0105     fclose(ctx->fp);
0106   }
0107 
0108   free(ctx);
0109   sc = pthread_setspecific(grp_key, NULL);
0110   _Assert_Unused_variable_equals(sc, 0);
0111 }