Line data Source code
1 : // Copyright (c) 2013-2021 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 the environment variable support.
22 : *
23 : * This file implements a variable which retrieves its value from the
24 : * process environment. For example, you could retrieve the path to
25 : * the HOME directory.
26 : *
27 : * This is often used to distinguish between runs.
28 : */
29 :
30 : // self
31 : //
32 : #include "snaplogger/variable.h"
33 :
34 :
35 : // C lib
36 : //
37 : #include <grp.h>
38 : #include <pwd.h>
39 : #include <sys/types.h>
40 : #include <unistd.h>
41 :
42 :
43 : // last include
44 : //
45 : #include <snapdev/poison.h>
46 :
47 :
48 :
49 : namespace snaplogger
50 : {
51 :
52 :
53 : namespace
54 : {
55 :
56 :
57 8 : DEFINE_LOGGER_VARIABLE(uid)
58 : {
59 0 : auto params(get_params());
60 0 : if(params.size() > 0
61 0 : && params[0]->get_name() == "running")
62 : {
63 0 : value += std::to_string(getuid());
64 : }
65 : else
66 : {
67 0 : value += std::to_string(msg.get_environment()->get_uid());
68 : }
69 :
70 0 : variable::process_value(msg, value);
71 0 : }
72 :
73 :
74 8 : DEFINE_LOGGER_VARIABLE(username)
75 : {
76 : uid_t uid;
77 0 : auto params(get_params());
78 0 : if(params.size() > 0
79 0 : && params[0]->get_name() == "running")
80 : {
81 0 : uid = getuid();
82 : }
83 : else
84 : {
85 0 : uid = msg.get_environment()->get_uid();
86 : }
87 :
88 0 : char buf[1024];
89 0 : passwd pw;
90 0 : passwd * pw_ptr(nullptr);
91 0 : if(getpwuid_r(uid, &pw, buf, sizeof(buf), &pw_ptr) == 0
92 0 : && pw_ptr == &pw)
93 : {
94 0 : value += pw.pw_name;
95 : }
96 :
97 0 : variable::process_value(msg, value);
98 0 : }
99 :
100 :
101 8 : DEFINE_LOGGER_VARIABLE(gid)
102 : {
103 0 : auto params(get_params());
104 0 : if(params.size() > 0
105 0 : && params[0]->get_name() == "running")
106 : {
107 0 : value += std::to_string(getgid());
108 : }
109 : else
110 : {
111 0 : value += std::to_string(msg.get_environment()->get_gid());
112 : }
113 :
114 0 : variable::process_value(msg, value);
115 0 : }
116 :
117 :
118 8 : DEFINE_LOGGER_VARIABLE(groupname)
119 : {
120 : gid_t gid;
121 0 : auto params(get_params());
122 0 : if(params.size() > 0
123 0 : && params[0]->get_name() == "running")
124 : {
125 0 : gid = getgid();
126 : }
127 : else
128 : {
129 0 : gid = msg.get_environment()->get_gid();
130 : }
131 :
132 0 : char buf[1024];
133 0 : group gr;
134 0 : group * gr_ptr(nullptr);
135 0 : if(getgrgid_r(gid, &gr, buf, sizeof(buf), &gr_ptr) == 0
136 0 : && gr_ptr == &gr)
137 : {
138 0 : value += gr.gr_name;
139 : }
140 :
141 0 : variable::process_value(msg, value);
142 0 : }
143 :
144 :
145 : }
146 : // no name namespace
147 :
148 :
149 6 : } // snaplogger namespace
150 : // vim: ts=4 sw=4 et
|