]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.c
Update projects
[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    Copyright (C) 2004 Kern Sibbald and John Walker
16
17    This program is free software; you can redistribute it and/or
18    modify it under the terms of the GNU General Public License as
19    published by the Free Software Foundation; either version 2 of
20    the License, or (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25    General Public License for more details.
26
27    You should have received a copy of the GNU General Public
28    License along with this program; if not, write to the Free
29    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30    MA 02111-1307, USA.
31
32  */
33
34 #include "bacula.h"
35
36 #ifndef HAVE_WIN32
37 extern const char *get_signal_name(int sig);
38 extern int num_execvp_errors;
39 extern int execvp_errors[];
40 #endif
41
42 const char *berrno::strerror()
43 {
44    int stat = 0;
45 #ifdef HAVE_WIN32
46    LPVOID msg;
47
48    if (berrno_ && b_errno_win32) {
49       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
50           FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
51           NULL,
52           GetLastError(),
53           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
54           (LPTSTR)&msg,
55           0,
56           NULL);
57
58       pm_strcpy(&buf_, (const char *)msg);
59       LocalFree(msg);
60       return (const char *)buf_;
61    }
62 #else
63    if (berrno_ & b_errno_exit) {
64       stat = (berrno_ & ~b_errno_exit);       /* remove bit */
65       if (stat == 0) {
66          return "Child exited normally.";    /* this really shouldn't happen */
67       } else {
68          /* Maybe an execvp failure */
69          if (stat >= 200) {
70             if (stat < 200 + num_execvp_errors) {
71                berrno_ = execvp_errors[stat - 200];
72             } else {
73                return "Unknown error during program execvp";
74             }
75          } else {
76             Mmsg(&buf_, "Child exited with code %d", stat);
77             return buf_;
78          }
79          /* If we drop out here, berrno_ is set to an execvp errno */
80       }
81    }
82    if (berrno_ & b_errno_signal) {
83       stat = (berrno_ & ~b_errno_signal);        /* remove bit */
84       Mmsg(&buf_, "Child died from signal %d: %s", stat, get_signal_name(stat));
85       return buf_;
86    }
87 #endif
88    /* Normal errno */
89    if (bstrerror(berrno_, buf_, 1024) < 0) {
90       return _("Invalid errno. No error message possible.");
91    }
92    return buf_;
93 }
94
95
96 #ifdef TEST_PROGRAM
97
98 int main()
99 {
100 }
101 #endif