]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.c
Update copyrights
[bacula/bacula] / bacula / src / lib / berrno.c
1 /*
2  *  Bacula errno handler
3  *
4  *    berrno is a simplistic errno handler that works for
5  *      Unix, Win32, and Bacula bpipes.
6  *
7  *    See berrno.h for how to use berrno.
8  *
9  *   Kern Sibbald, July MMIV
10  *
11  *   Version $Id$
12  *
13  */
14 /*
15    Bacula® - The Network Backup Solution
16
17    Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
18
19    The main author of Bacula is Kern Sibbald, with contributions from
20    many others, a complete list can be found in the file AUTHORS.
21    This program is Free Software; you can redistribute it and/or
22    modify it under the terms of version two of the GNU General Public
23    License as published by the Free Software Foundation plus additions
24    that are listed in the file LICENSE.
25
26    This program is distributed in the hope that it will be useful, but
27    WITHOUT ANY WARRANTY; without even the implied warranty of
28    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29    General Public License for more details.
30
31    You should have received a copy of the GNU General Public License
32    along with this program; if not, write to the Free Software
33    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34    02110-1301, USA.
35
36    Bacula® is a registered trademark of John Walker.
37    The licensor of Bacula is the Free Software Foundation Europe
38    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
39    Switzerland, email:ftf@fsfeurope.org.
40 */
41
42 #include "bacula.h"
43
44 #ifndef HAVE_WIN32
45 extern const char *get_signal_name(int sig);
46 extern int num_execvp_errors;
47 extern int execvp_errors[];
48 #endif
49
50 const char *berrno::strerror()
51 {
52 #ifdef HAVE_WIN32
53    if (berrno_ & b_errno_win32) {
54       return (const char *)buf_;
55    }
56 #else
57    int stat = 0;
58
59    if (berrno_ & b_errno_exit) {
60       stat = (berrno_ & ~b_errno_exit);       /* remove bit */
61       if (stat == 0) {
62          return _("Child exited normally.");    /* this really shouldn't happen */
63       } else {
64          /* Maybe an execvp failure */
65          if (stat >= 200) {
66             if (stat < 200 + num_execvp_errors) {
67                berrno_ = execvp_errors[stat - 200];
68             } else {
69                return _("Unknown error during program execvp");
70             }
71          } else {
72             Mmsg(buf_, _("Child exited with code %d"), stat);
73             return buf_;
74          }
75          /* If we drop out here, berrno_ is set to an execvp errno */
76       }
77    }
78    if (berrno_ & b_errno_signal) {
79       stat = (berrno_ & ~b_errno_signal);        /* remove bit */
80       Mmsg(buf_, _("Child died from signal %d: %s"), stat, get_signal_name(stat));
81       return buf_;
82    }
83 #endif
84    /* Normal errno */
85    if (bstrerror(berrno_, buf_, 1024) < 0) {
86       return _("Invalid errno. No error message possible.");
87    }
88    return buf_;
89 }
90
91 void berrno::format_win32_message()
92 {
93 #ifdef HAVE_WIN32
94    LPVOID msg;
95    if (berrno_ & b_errno_win32) {
96       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
97           FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
98           NULL,
99           GetLastError(),
100           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
101           (LPTSTR)&msg,
102           0,
103           NULL);
104
105       pm_strcpy(&buf_, (const char *)msg);
106       LocalFree(msg);
107    }
108 #endif
109 }
110
111 #ifdef TEST_PROGRAM
112
113 int main()
114 {
115 }
116 #endif