]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/berrno.h
Change copyright as per agreement with FSFE
[bacula/bacula] / bacula / src / lib / berrno.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many 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    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  * Kern Sibbald, July MMIV
22  *
23  */
24
25 /*
26  * Extra bits set to interpret errno value differently from errno
27  */
28 #ifdef HAVE_WIN32
29 #define b_errno_win32  (1<<29)        /* user reserved bit */
30 #define b_errno_WSA    (1<<26)
31 #else
32 #define b_errno_win32  0              /* On Unix/Linix system */
33 #endif
34 #define b_errno_exit   (1<<28)        /* child exited, exit code returned */
35 #define b_errno_signal (1<<27)        /* child died, signal code returned */
36
37 /*
38  * A more generalized way of handling errno that works with Unix, Windows,
39  *  and with Bacula bpipes.
40  *
41  * It works by picking up errno and creating a memory pool buffer
42  *  for editing the message. strerror() does the actual editing, and
43  *  it is thread safe.
44  *
45  * If bit 29 in m_berrno is set then it is a Win32 error, and we
46  *  must do a GetLastError() to get the error code for formatting.
47  * If bit 29 in m_berrno is not set, then it is a Unix errno.
48  *
49  */
50 class berrno : public SMARTALLOC {
51    POOLMEM *m_buf;
52    int m_berrno;
53    void format_win32_message();
54 public:
55    berrno(int pool=PM_EMSG);
56    ~berrno();
57    const char *bstrerror();
58    const char *bstrerror(int errnum);
59    void set_errno(int errnum);
60    int code() { return m_berrno & ~(b_errno_exit|b_errno_signal); }
61    int code(int stat) { return stat & ~(b_errno_exit|b_errno_signal); }
62 };
63
64 /* Constructor */
65 inline berrno::berrno(int pool)
66 {
67    m_berrno = errno;
68    m_buf = get_pool_memory(pool);
69    *m_buf = 0;
70    errno = m_berrno;
71 }
72
73 inline berrno::~berrno()
74 {
75    free_pool_memory(m_buf);
76 }
77
78 inline const char *berrno::bstrerror(int errnum)
79 {
80    m_berrno = errnum;
81    return berrno::bstrerror();
82 }
83
84
85 inline void berrno::set_errno(int errnum)
86 {
87    m_berrno = errnum;
88 }