]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.c
Ensure b_sterror() is using the correct size of input buffer
[bacula/bacula] / bacula / src / lib / berrno.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    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    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *  Bacula errno handler
18  *
19  *    berrno is a simplistic errno handler that works for
20  *      Unix, Win32, and Bacula bpipes.
21  *
22  *    See berrno.h for how to use berrno.
23  *
24  *   Written by Kern Sibbald, July MMIV
25  *
26  *
27  */
28
29 #include "bacula.h"
30
31 #ifndef HAVE_WIN32
32 extern const char *get_signal_name(int sig);
33 extern int num_execvp_errors;
34 extern int execvp_errors[];
35 #endif
36
37 const char *berrno::bstrerror()
38 {
39    *m_buf = 0;
40 #ifdef HAVE_WIN32
41    if (m_berrno & (b_errno_win32)) {
42       format_win32_message();
43       return (const char *)m_buf;
44    }
45 #else
46    int stat = 0;
47
48    if (m_berrno & b_errno_exit) {
49       stat = (m_berrno & ~b_errno_exit);       /* remove bit */
50       if (stat == 0) {
51          return _("Child exited normally.");    /* this really shouldn't happen */
52       } else {
53          /* Maybe an execvp failure */
54          if (stat >= 200) {
55             if (stat < 200 + num_execvp_errors) {
56                m_berrno = execvp_errors[stat - 200];
57             } else {
58                return _("Unknown error during program execvp");
59             }
60          } else {
61             Mmsg(m_buf, _("Child exited with code %d"), stat);
62             return m_buf;
63          }
64          /* If we drop out here, m_berrno is set to an execvp errno */
65       }
66    }
67    if (m_berrno & b_errno_signal) {
68       stat = (m_berrno & ~b_errno_signal);        /* remove bit */
69       Mmsg(m_buf, _("Child died from signal %d: %s"), stat, get_signal_name(stat));
70       return m_buf;
71    }
72 #endif
73    /* Normal errno */
74    if (b_strerror(m_berrno, m_buf, sizeof_pool_memory(m_buf)) < 0) {
75       return _("Invalid errno. No error message possible.");
76    }
77    return m_buf;
78 }
79
80 void berrno::format_win32_message()
81 {
82 #ifdef HAVE_WIN32
83    LPVOID msg;
84    FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
85        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
86        NULL,
87        GetLastError(),
88        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
89        (LPTSTR)&msg,
90        0,
91        NULL);
92    pm_strcpy(&m_buf, (const char *)msg);
93    LocalFree(msg);
94 #endif
95 }
96
97 #ifdef TEST_PROGRAM
98
99 int main()
100 {
101 }
102 #endif