1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2004-2009 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2019 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, July MMIV
24  */
25 /**
26  * @file
27  * BErrNo header file
28  */
29 
30 #ifndef BAREOS_LIB_BERRNO_H_
31 #define BAREOS_LIB_BERRNO_H_
32 
33 #include "lib/berrno.h"
34 #include "include/bareos.h"
35 
36 /**
37  * Extra bits set to interpret errno value differently from errno
38  */
39 #ifdef HAVE_WIN32
40 #  define b_errno_win32 (1 << 29) /* user reserved bit */
41 #else
42 #  define b_errno_win32 0 /* On Unix/Linix system */
43 #endif
44 #define b_errno_exit (1 << 28)   /* child exited, exit code returned */
45 #define b_errno_signal (1 << 27) /* child died, signal code returned */
46 
47 /**
48  * A more generalized way of handling errno that works with Unix, Windows,
49  *  and with BAREOS bpipes.
50  *
51  * It works by picking up errno and creating a memory pool buffer
52  *  for editing the message. strerror() does the actual editing, and
53  *  it is thread safe.
54  *
55  * If bit 29 in berrno_ is set then it is a Win32 error, and we
56  *  must do a GetLastError() to get the error code for formatting.
57  * If bit 29 in berrno_ is not set, then it is a Unix errno.
58  *
59  */
60 class BErrNo {
61   POOLMEM* buf_;
62   int berrno_;
63   void FormatWin32Message();
64 
65  public:
66   BErrNo(int pool = PM_EMSG);
67   ~BErrNo();
68   const char* bstrerror();
69   const char* bstrerror(int errnum);
70   void SetErrno(int errnum);
code()71   int code() { return berrno_ & ~(b_errno_exit | b_errno_signal); }
code(int stat)72   int code(int stat) { return stat & ~(b_errno_exit | b_errno_signal); }
73 };
74 
75 /* Constructor */
BErrNo(int pool)76 inline BErrNo::BErrNo(int pool)
77 {
78   berrno_ = errno;
79   buf_ = GetPoolMemory(pool);
80   *buf_ = 0;
81   errno = berrno_;
82 }
83 
~BErrNo()84 inline BErrNo::~BErrNo() { FreePoolMemory(buf_); }
85 
bstrerror(int errnum)86 inline const char* BErrNo::bstrerror(int errnum)
87 {
88   berrno_ = errnum;
89   return BErrNo::bstrerror();
90 }
91 
92 
SetErrno(int errnum)93 inline void BErrNo::SetErrno(int errnum) { berrno_ = errnum; }
94 
95 #endif /* BAREOS_LIB_BERRNO_H_ */
96