LCOV - code coverage report
Current view: top level - snaplogger - user_variable.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 41 12.2 %
Date: 2019-12-13 00:59:36 Functions: 14 38 36.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13