1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2019-2019 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 #include "include/hostconfig.h"
22 #include <pthread.h>
23 #if defined(HAVE_PTHREAD_NP_H)
24 #include <pthread_np.h>
25 #endif
26 #include "pthread_detach_if_not_detached.h"
27 
28 namespace directordaemon {
DetachIfNotDetached(pthread_t thr)29 void DetachIfNotDetached(pthread_t thr)
30 {
31 #if defined(HAVE_WIN32)
32   pthread_detach(thr);
33 #else
34   /* only detach if not yet detached */
35   int _detachstate;
36   pthread_attr_t _gattr;
37 #if defined(HAVE_PTHREAD_ATTR_GET_NP)
38   pthread_attr_init(&_gattr);
39   pthread_attr_get_np(thr, &_gattr);
40 #else
41   pthread_getattr_np(thr, &_gattr);
42 #endif // defined(HAVE_PTHREAD_ATTR_GET_NP)
43   pthread_attr_getdetachstate(&_gattr, &_detachstate);
44   pthread_attr_destroy(&_gattr);
45   if (_detachstate != PTHREAD_CREATE_DETACHED) { pthread_detach(thr); }
46 #endif // defined(HAVE_WIN32)
47 }
48 }  // namespace directordaemon
49