1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #include <mutex.h>
26 
27 #include <logging.h>                                            /* Log */
28 #include <cleanup.h>
29 
30 
__ThreadLock(pthread_mutex_t * mutex,const char * funcname,const char * filename,int lineno)31 void __ThreadLock(pthread_mutex_t *mutex,
32                   const char *funcname, const char *filename, int lineno)
33 
34 {
35     int result = pthread_mutex_lock(mutex);
36 
37     if (result != 0)
38     {
39         /* Since Log blocks on mutexes, using it would be unsafe. Therefore,
40            we use fprintf instead */
41         fprintf(stderr,
42                 "Locking failure at %s:%d function %s! "
43                 "(pthread_mutex_lock: %s)",
44                 filename, lineno, funcname, GetErrorStrFromCode(result));
45         fflush(stdout);
46         fflush(stderr);
47         DoCleanupAndExit(101);
48     }
49 }
50 
__ThreadUnlock(pthread_mutex_t * mutex,const char * funcname,const char * filename,int lineno)51 void __ThreadUnlock(pthread_mutex_t *mutex,
52                     const char *funcname, const char *filename, int lineno)
53 {
54     int result = pthread_mutex_unlock(mutex);
55 
56     if (result != 0)
57     {
58         /* Since Log blocks on mutexes, using it would be unsafe. Therefore,
59            we use fprintf instead */
60         fprintf(stderr,
61                 "Locking failure at %s:%d function %s! "
62                 "(pthread_mutex_unlock: %s)",
63                 filename, lineno, funcname, GetErrorStrFromCode(result));
64         fflush(stdout);
65         fflush(stderr);
66         DoCleanupAndExit(101);
67     }
68 }
69 
__ThreadWait(pthread_cond_t * pcond,pthread_mutex_t * mutex,int timeout,const char * funcname,const char * filename,int lineno)70 int __ThreadWait(pthread_cond_t *pcond, pthread_mutex_t *mutex, int timeout,
71                     const char *funcname, const char *filename, int lineno)
72 {
73     int result = 0;
74 
75     if (timeout == THREAD_BLOCK_INDEFINITELY)
76     {
77         result = pthread_cond_wait(pcond, mutex);
78     }
79     else
80     {
81         struct timespec ts;
82         clock_gettime(CLOCK_REALTIME, &ts);
83 
84         ts.tv_sec += timeout;
85         result = pthread_cond_timedwait(pcond, mutex, &ts);
86     }
87 
88     if (result != 0)
89     {
90         if (result == ETIMEDOUT)
91         {
92             Log(LOG_LEVEL_DEBUG,
93                 "Thread condition timed out at %s:%d function %s! "
94                 "(pthread_cond_timewait): %s)",
95                 filename, lineno, funcname, GetErrorStrFromCode(result));
96         }
97         else
98         {
99             /* Since Log blocks on mutexes, using it would be unsafe.
100                Therefore, we use fprintf instead */
101             fprintf(stderr,
102                     "Failed to wait for thread condition at %s:%d function "
103                     "%s! (pthread_cond_(wait|timewait)): %s)",
104                     filename, lineno, funcname, GetErrorStrFromCode(result));
105             fflush(stdout);
106             fflush(stderr);
107             DoCleanupAndExit(101);
108         }
109     }
110 
111     return result;
112 }
113