1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2004-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2016 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, July MMIV
24  */
25 /**
26  * @file
27  * BAREOS errno handler
28  *
29  * BErrNo is a simplistic errno handler that works for
30  * Unix, Win32, and BAREOS bpipes.
31  *
32  * See BErrNo.h for how to use BErrNo.
33  */
34 
35 #include "lib/berrno.h"
36 #include "lib/bsys.h"
37 
38 #ifndef HAVE_WIN32
39 extern const char* get_signal_name(int sig);
40 extern int num_execvp_errors;
41 extern int execvp_errors[];
42 #endif
43 
bstrerror()44 const char* BErrNo::bstrerror()
45 {
46   *buf_ = 0;
47 #ifdef HAVE_WIN32
48   if (berrno_ & b_errno_win32) {
49     FormatWin32Message();
50     return (const char*)buf_;
51   }
52 #else
53   int status = 0;
54 
55   if (berrno_ & b_errno_exit) {
56     status = (berrno_ & ~b_errno_exit); /* remove bit */
57     if (status == 0) {
58       return _("Child exited normally."); /* this really shouldn't happen */
59     } else {
60       /* Maybe an execvp failure */
61       if (status >= 200) {
62         if (status < 200 + num_execvp_errors) {
63           berrno_ = execvp_errors[status - 200];
64         } else {
65           return _("Unknown error during program execvp");
66         }
67       } else {
68         Mmsg(buf_, _("Child exited with code %d"), status);
69         return buf_;
70       }
71       /* If we drop out here, berrno_ is set to an execvp errno */
72     }
73   }
74   if (berrno_ & b_errno_signal) {
75     status = (berrno_ & ~b_errno_signal); /* remove bit */
76     Mmsg(buf_, _("Child died from signal %d: %s"), status,
77          get_signal_name(status));
78     return buf_;
79   }
80 #endif
81   /* Normal errno */
82   if (b_strerror(berrno_, buf_, 1024) < 0) {
83     return _("Invalid errno. No error message possible.");
84   }
85   return buf_;
86 }
87 
FormatWin32Message()88 void BErrNo::FormatWin32Message()
89 {
90 #ifdef HAVE_WIN32
91   LPVOID msg;
92   FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
93                      FORMAT_MESSAGE_IGNORE_INSERTS,
94                  NULL, GetLastError(),
95                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0,
96                  NULL);
97   PmStrcpy(buf_, (const char*)msg);
98   LocalFree(msg);
99 #endif
100 }
101