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