Line data Source code
1 : /*
2 : * DUNE DAQ modification notice:
3 : * This file has been modified from the original ATLAS ers source for the DUNE DAQ project.
4 : * Fork baseline commit: 8267df82a4f6fe6bf02c4014923eba19eddc4614 (2020-04-14).
5 : * Renamed since fork: yes (from src/LocalContext.cxx to src/LocalContext.cpp).
6 : *
7 : * Original copyright:
8 : * Copyright (C) 2001-2020 CERN for the benefit of the ATLAS collaboration.
9 : * Licensed under the Apache License, Version 2.0.
10 : */
11 :
12 : /*
13 : * LocalContext.cxx
14 : * ers
15 : *
16 : * Created by Serguei Kolos on 26.11.05.
17 : * Copyright 2004 CERN. All rights reserved.
18 : *
19 : */
20 : #include <pwd.h>
21 : #include <stdlib.h>
22 : #include <sys/types.h>
23 : #include <unistd.h>
24 :
25 : #include <iterator>
26 :
27 : #include <ers/LocalContext.hpp>
28 :
29 : #if !defined(__APPLE__) && !defined(__rtems__)
30 : #include <execinfo.h>
31 : #include <sys/syscall.h>
32 : #else
33 : int
34 : backtrace(void**, int)
35 : {
36 : return 0;
37 : }
38 : #endif
39 :
40 : pid_t
41 3390 : gettid()
42 : {
43 : #if !defined(__APPLE__) && !defined(__rtems__)
44 3390 : return syscall(SYS_gettid);
45 : #else
46 : return 0;
47 : #endif
48 : }
49 :
50 : namespace {
51 : const char*
52 78 : get_cwd()
53 : {
54 234 : static std::string buf;
55 78 : if (buf.empty()) {
56 78 : char tmp[1024];
57 78 : if (::getcwd(tmp, sizeof(tmp)))
58 78 : buf = tmp;
59 : }
60 78 : return buf.c_str();
61 : }
62 :
63 : const char*
64 78 : get_user_name()
65 : {
66 156 : static std::string buf;
67 78 : if (buf.empty()) {
68 78 : struct passwd* psw = ::getpwuid(geteuid());
69 78 : if (psw)
70 78 : buf = psw->pw_name;
71 : }
72 78 : return buf.c_str();
73 : }
74 :
75 : const char*
76 78 : get_host_name()
77 : {
78 156 : static std::string buf;
79 78 : if (buf.empty()) {
80 78 : char tmp[1024];
81 78 : if (!::gethostname(tmp, sizeof(tmp)))
82 78 : buf = tmp;
83 : }
84 78 : return buf.c_str();
85 : }
86 : }
87 :
88 3390 : ers::LocalContext::LocalContext(const char* package_name,
89 : const char* filename,
90 : int line_number,
91 : const char* function_name,
92 3390 : bool debug)
93 3390 : : m_package_name(package_name)
94 3390 : , m_file_name(filename)
95 3390 : , m_function_name(function_name)
96 3390 : , m_line_number(line_number)
97 6780 : , m_thread_id(gettid())
98 3390 : , m_stack_size(debug ? backtrace(m_stack, std::size(m_stack)) : 0)
99 : {
100 3390 : ;
101 3390 : }
102 :
103 : const char*
104 0 : ers::LocalContext::application_name() const
105 : {
106 0 : static int pid = ::getpid();
107 0 : static const char* env = ::getenv("DUNEDAQ_APPLICATION_NAME") ? ::getenv("DUNEDAQ_APPLICATION_NAME") : "Undefined";
108 :
109 0 : if (pid != ::getpid()) {
110 0 : pid = ::getpid();
111 0 : env = ::getenv("DUNEDAQ_APPLICATION_NAME") ? ::getenv("DUNEDAQ_APPLICATION_NAME") : "Undefined";
112 : }
113 0 : return env;
114 : }
115 :
116 : const ers::LocalProcessContext ers::LocalContext::c_process(get_host_name(), get_cwd(), geteuid(), get_user_name());
|