LCOV - differential code coverage report
Current view: top level - src/common - username.c (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage HEAD vs 15 Lines: 68.8 % 16 11 5 11
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 2 2 2
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

           TLA  Line data    Source code
       1                 : /*-------------------------------------------------------------------------
       2                 :  *
       3                 :  * username.c
       4                 :  *    get user name
       5                 :  *
       6                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7                 :  * Portions Copyright (c) 1994, Regents of the University of California
       8                 :  *
       9                 :  * IDENTIFICATION
      10                 :  *    src/common/username.c
      11                 :  *
      12                 :  *-------------------------------------------------------------------------
      13                 :  */
      14                 : 
      15                 : #ifndef FRONTEND
      16                 : #include "postgres.h"
      17                 : #else
      18                 : #include "postgres_fe.h"
      19                 : #endif
      20                 : 
      21                 : #include <pwd.h>
      22                 : #include <unistd.h>
      23                 : 
      24                 : #include "common/username.h"
      25                 : 
      26                 : /*
      27                 :  * Returns the current user name in a static buffer
      28                 :  * On error, returns NULL and sets *errstr to point to a palloc'd message
      29                 :  */
      30                 : const char *
      31 CBC         649 : get_user_name(char **errstr)
      32                 : {
      33                 : #ifndef WIN32
      34                 :     struct passwd *pw;
      35             649 :     uid_t       user_id = geteuid();
      36                 : 
      37             649 :     *errstr = NULL;
      38                 : 
      39             649 :     errno = 0;                  /* clear errno before call */
      40             649 :     pw = getpwuid(user_id);
      41             649 :     if (!pw)
      42                 :     {
      43 UBC           0 :         *errstr = psprintf(_("could not look up effective user ID %ld: %s"),
      44                 :                            (long) user_id,
      45               0 :                            errno ? strerror(errno) : _("user does not exist"));
      46               0 :         return NULL;
      47                 :     }
      48                 : 
      49 CBC         649 :     return pw->pw_name;
      50                 : #else
      51                 :     /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
      52                 :     /* "static" variable remains after function exit */
      53                 :     static char username[256 + 1];
      54                 :     DWORD       len = sizeof(username);
      55                 : 
      56                 :     *errstr = NULL;
      57                 : 
      58                 :     if (!GetUserName(username, &len))
      59                 :     {
      60                 :         *errstr = psprintf(_("user name lookup failure: error code %lu"),
      61                 :                            GetLastError());
      62                 :         return NULL;
      63                 :     }
      64                 : 
      65                 :     return username;
      66                 : #endif
      67                 : }
      68                 : 
      69                 : 
      70                 : /*
      71                 :  * Returns the current user name in a static buffer or exits
      72                 :  */
      73                 : const char *
      74             642 : get_user_name_or_exit(const char *progname)
      75                 : {
      76                 :     const char *user_name;
      77                 :     char       *errstr;
      78                 : 
      79             642 :     user_name = get_user_name(&errstr);
      80                 : 
      81             642 :     if (!user_name)
      82                 :     {
      83 UBC           0 :         fprintf(stderr, "%s: %s\n", progname, errstr);
      84               0 :         exit(1);
      85                 :     }
      86 CBC         642 :     return user_name;
      87                 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a