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 support.
24 : *
25 : * Whenever a log is emitted, an environment is attached to it. It is
26 : * very important since the environment of the appender (the one used
27 : * to format the message) may be different than the environment where
28 : * the message was emitted.
29 : *
30 : * Examples:
31 : *
32 : * 1. if you use asynchronous logging, then the tid changes, we use a
33 : * separate thread to work on the log so the tid is going to be different
34 : * from the tid of the emitter
35 : * 2. if you use the network feature, the appenders are going to be run
36 : * on a completely different computer through a log service which is
37 : * likely to have a different PID, UID, TID, domain name, hostname, etc.
38 : *
39 : * Environments are used through smart pointers, if one changes the
40 : * ones in existing messages do not change.
41 : */
42 :
43 : // self
44 : //
45 : #include "snaplogger/environment.h"
46 :
47 : #include "snaplogger/guard.h"
48 : #include "snaplogger/map_diagnostic.h"
49 : #include "snaplogger/private_logger.h"
50 :
51 :
52 : // C lib
53 : //
54 : #include <grp.h>
55 : #include <netdb.h>
56 : #include <pwd.h>
57 : #include <sys/param.h>
58 : #include <sys/sysinfo.h>
59 : #include <sys/types.h>
60 :
61 :
62 : // last include
63 : //
64 : #include <snapdev/poison.h>
65 :
66 :
67 :
68 : namespace snaplogger
69 : {
70 :
71 :
72 :
73 1 : environment::environment(pid_t tid)
74 1 : : f_tid(tid)
75 : {
76 1 : f_uid = getuid();
77 1 : f_pid = getpid();
78 1 : f_gid = getgid();
79 :
80 : char buf[1024];
81 : passwd pw;
82 1 : passwd * pw_ptr(nullptr);
83 2 : if(getpwuid_r(f_uid, &pw, buf, sizeof(buf), &pw_ptr) == 0
84 1 : && pw_ptr == &pw)
85 : {
86 1 : f_username = pw.pw_name;
87 : }
88 :
89 : group gr;
90 1 : group * gr_ptr(nullptr);
91 2 : if(getgrgid_r(f_gid, &gr, buf, sizeof(buf), &gr_ptr) == 0
92 1 : && gr_ptr == &gr)
93 : {
94 1 : f_groupname += gr.gr_name;
95 : }
96 :
97 : char host_buffer[HOST_NAME_MAX + 2];
98 1 : host_buffer[HOST_NAME_MAX + 1] = '\0'; // make sure it's null terminated
99 :
100 1 : if(gethostname(host_buffer, HOST_NAME_MAX + 1) == 0)
101 : {
102 1 : f_hostname = host_buffer;
103 : }
104 :
105 1 : if(getdomainname(host_buffer, HOST_NAME_MAX + 1) == 0)
106 : {
107 1 : f_domainname = host_buffer;
108 : }
109 :
110 2 : map_diagnostics_t const diag(get_map_diagnostics());
111 :
112 1 : auto const prog_it(diag.find("progname"));
113 1 : if(prog_it != diag.end())
114 : {
115 1 : f_progname = prog_it->second;
116 : }
117 :
118 2 : std::string const tid_str(std::to_string(tid));
119 1 : auto const thread_it(diag.find("threadname#" + tid_str));
120 1 : if(thread_it != diag.end())
121 : {
122 0 : f_threadname = thread_it->second;
123 : }
124 1 : }
125 :
126 :
127 :
128 0 : uid_t environment::get_uid() const
129 : {
130 0 : return f_uid;
131 : }
132 :
133 :
134 1 : pid_t environment::get_pid() const
135 : {
136 1 : return f_pid;
137 : }
138 :
139 :
140 0 : gid_t environment::get_gid() const
141 : {
142 0 : return f_gid;
143 : }
144 :
145 :
146 0 : pid_t environment::get_tid() const
147 : {
148 0 : return f_tid;
149 : }
150 :
151 :
152 0 : std::string environment::get_username() const
153 : {
154 0 : guard g;
155 :
156 0 : return f_username;
157 : }
158 :
159 :
160 0 : std::string environment::get_groupname() const
161 : {
162 0 : guard g;
163 :
164 0 : return f_groupname;
165 : }
166 :
167 :
168 28 : std::string environment::get_hostname() const
169 : {
170 56 : guard g;
171 :
172 56 : return f_hostname;
173 : }
174 :
175 :
176 0 : std::string environment::get_domainname() const
177 : {
178 0 : guard g;
179 :
180 0 : return f_domainname;
181 : }
182 :
183 :
184 0 : std::string environment::get_progname() const
185 : {
186 0 : guard g;
187 :
188 0 : return f_progname;
189 : }
190 :
191 :
192 0 : std::string environment::get_threadname() const
193 : {
194 0 : guard g;
195 :
196 0 : return f_threadname;
197 : }
198 :
199 :
200 :
201 :
202 :
203 :
204 94615 : environment::pointer_t create_environment()
205 : {
206 94615 : return get_private_logger()->create_environment();
207 : }
208 :
209 :
210 :
211 :
212 :
213 : } // snaplogger namespace
214 : // vim: ts=4 sw=4 et
|