LCOV - code coverage report
Current view: top level - snaplogger - user_variable.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 45 11.1 %
Date: 2022-07-01 22:43:09 Functions: 14 42 33.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-2022  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/snaplogger
       4             : // contact@m2osw.com
       5             : //
       6             : // This program is free software; you can redistribute it and/or modify
       7             : // it under the terms of the GNU General Public License as published by
       8             : // the Free Software Foundation; either version 2 of the License, or
       9             : // (at your option) any later version.
      10             : //
      11             : // This program is distributed in the hope that it will be useful,
      12             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             : // GNU General Public License for more details.
      15             : //
      16             : // You should have received a copy of the GNU General Public License along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             : 
      20             : /** \file
      21             :  * \brief Implementation of user related variables.
      22             :  *
      23             :  * This file implements support for data intrinsic to the current user.
      24             :  *
      25             :  * \li `uid`
      26             :  *
      27             :  * The user identifier as found in `/etc/passwd`.
      28             :  *
      29             :  * \li `username`
      30             :  *
      31             :  * The name of the current user as defined in the password database.
      32             :  * This function searches for a name for the current getuid().
      33             :  *
      34             :  * \li `gid`
      35             :  *
      36             :  * The group identifier of the current user as found in `/etc/group`.
      37             :  *
      38             :  * \li `groupname`
      39             :  *
      40             :  * The name of the current group as defined in the group database.
      41             :  * This function searches for a name for the current getgid().
      42             :  */
      43             : 
      44             : // self
      45             : //
      46             : #include    "snaplogger/variable.h"
      47             : 
      48             : 
      49             : // C lib
      50             : //
      51             : #include    <grp.h>
      52             : #include    <pwd.h>
      53             : #include    <sys/types.h>
      54             : #include    <unistd.h>
      55             : 
      56             : 
      57             : // last include
      58             : //
      59             : #include    <snapdev/poison.h>
      60             : 
      61             : 
      62             : 
      63             : namespace snaplogger
      64             : {
      65             : 
      66             : 
      67             : namespace
      68             : {
      69             : 
      70             : 
      71           7 : DEFINE_LOGGER_VARIABLE(uid)
      72             : {
      73           0 :     auto params(get_params());
      74           0 :     if(params.size() > 0
      75           0 :     && params[0]->get_name() == "running")
      76             :     {
      77           0 :         value += std::to_string(getuid());
      78             :     }
      79             :     else
      80             :     {
      81           0 :         value += std::to_string(msg.get_environment()->get_uid());
      82             :     }
      83             : 
      84           0 :     variable::process_value(msg, value);
      85           0 : }
      86             : 
      87             : 
      88           7 : DEFINE_LOGGER_VARIABLE(username)
      89             : {
      90             :     uid_t uid;
      91           0 :     auto params(get_params());
      92           0 :     if(params.size() > 0
      93           0 :     && params[0]->get_name() == "running")
      94             :     {
      95           0 :         uid = getuid();
      96             :     }
      97             :     else
      98             :     {
      99           0 :         uid = msg.get_environment()->get_uid();
     100             :     }
     101             : 
     102           0 :     char buf[1024];
     103           0 :     passwd pw;
     104           0 :     passwd * pw_ptr(nullptr);
     105           0 :     if(getpwuid_r(uid, &pw, buf, sizeof(buf), &pw_ptr) == 0
     106           0 :     && pw_ptr == &pw)
     107             :     {
     108           0 :         value += pw.pw_name;
     109             :     }
     110             : 
     111           0 :     variable::process_value(msg, value);
     112           0 : }
     113             : 
     114             : 
     115           7 : DEFINE_LOGGER_VARIABLE(gid)
     116             : {
     117           0 :     auto params(get_params());
     118           0 :     if(params.size() > 0
     119           0 :     && params[0]->get_name() == "running")
     120             :     {
     121           0 :         value += std::to_string(getgid());
     122             :     }
     123             :     else
     124             :     {
     125           0 :         value += std::to_string(msg.get_environment()->get_gid());
     126             :     }
     127             : 
     128           0 :     variable::process_value(msg, value);
     129           0 : }
     130             : 
     131             : 
     132           7 : DEFINE_LOGGER_VARIABLE(groupname)
     133             : {
     134             :     gid_t gid;
     135           0 :     auto params(get_params());
     136           0 :     if(params.size() > 0
     137           0 :     && params[0]->get_name() == "running")
     138             :     {
     139           0 :         gid = getgid();
     140             :     }
     141             :     else
     142             :     {
     143           0 :         gid = msg.get_environment()->get_gid();
     144             :     }
     145             : 
     146           0 :     char buf[1024];
     147           0 :     group gr;
     148           0 :     group * gr_ptr(nullptr);
     149           0 :     if(getgrgid_r(gid, &gr, buf, sizeof(buf), &gr_ptr) == 0
     150           0 :     && gr_ptr == &gr)
     151             :     {
     152           0 :         value += gr.gr_name;
     153             :     }
     154             : 
     155           0 :     variable::process_value(msg, value);
     156           0 : }
     157             : 
     158             : 
     159             : }
     160             : // no name namespace
     161             : 
     162             : 
     163           6 : } // snaplogger namespace
     164             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13