1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Bacula errno handler
21  *
22  *    berrno is a simplistic errno handler that works for
23  *      Unix, Win32, and Bacula bpipes.
24  *
25  *    See berrno.h for how to use berrno.
26  *
27  *   Kern Sibbald, July MMIV
28  *
29  *
30  */
31 
32 #include "bacula.h"
33 
34 #ifndef HAVE_WIN32
35 extern const char *get_signal_name(int sig);
36 extern int num_execvp_errors;
37 extern int execvp_errors[];
38 #endif
39 
bstrerror()40 const char *berrno::bstrerror()
41 {
42    *m_buf = 0;
43 #ifdef HAVE_WIN32
44    if (m_berrno & (b_errno_win32 | b_errno_WSA)) {
45       format_win32_message();
46       return (const char *)m_buf;
47    }
48 #else
49    int stat = 0;
50 
51    if (m_berrno & b_errno_exit) {
52       stat = (m_berrno & ~b_errno_exit);       /* remove bit */
53       if (stat == 0) {
54          return _("Child exited normally.");    /* this really shouldn't happen */
55       } else {
56          /* Maybe an execvp failure */
57          if (stat >= 200) {
58             if (stat < 200 + num_execvp_errors) {
59                m_berrno = execvp_errors[stat - 200];
60             } else {
61                return _("Unknown error during program execvp");
62             }
63          } else {
64             Mmsg(m_buf, _("Child exited with code %d"), stat);
65             return m_buf;
66          }
67          /* If we drop out here, m_berrno is set to an execvp errno */
68       }
69    }
70    if (m_berrno & b_errno_signal) {
71       stat = (m_berrno & ~b_errno_signal);        /* remove bit */
72       Mmsg(m_buf, _("Child died from signal %d: %s"), stat, get_signal_name(stat));
73       return m_buf;
74    }
75 #endif
76    /* Normal errno */
77    if (b_strerror(m_berrno, m_buf, sizeof_pool_memory(m_buf)) < 0) {
78       return _("Invalid errno. No error message possible.");
79    }
80    return m_buf;
81 }
82 
format_win32_message()83 void berrno::format_win32_message()
84 {
85 #ifdef HAVE_WIN32
86    LPVOID msg;
87    FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
88        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
89        NULL,
90        m_berrno & b_errno_WSA ? WSAGetLastError() : GetLastError(),
91        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
92        (LPTSTR)&msg,
93        0,
94        NULL);
95    pm_strcpy(&m_buf, (const char *)msg);
96    LocalFree(msg);
97 #endif
98 }
99 
100 #ifdef TEST_PROGRAM
101 
main()102 int main()
103 {
104 }
105 #endif
106