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-08-13 00:35:33 Functions: 14 38 36.8 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.12