cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

conn_cache.c
Go to the documentation of this file.
1 /*
2  Copyright 2019 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 
26 #include <platform.h>
27 #include <conn_cache.h>
28 
29 #include <cfnet.h> /* AgentConnection */
30 #include <client_code.h> /* DisconnectServer */
31 #include <sequence.h> /* Seq */
32 #include <mutex.h> /* ThreadLock */
33 #include <communication.h> /* Hostname2IPString */
34 #include <misc_lib.h> /* CF_ASSERT */
35 #include <string_lib.h> /* StringEqual */
36 
37 
38 /**
39  Global cache for connections to servers, currently only used in cf-agent.
40 
41  @note THREAD-SAFETY: yes this connection cache *is* thread-safe, but is
42  extremely slow if used intensely from multiple threads. It needs to
43  be redesigned from scratch for that, not a priority for
44  single-threaded cf-agent!
45 */
46 
47 
48 typedef struct
49 {
51  enum ConnCacheStatus status; /* TODO unify with conn->conn_info->status */
53 
54 
56 
57 static Seq *conn_cache = NULL;
58 
59 
61 {
63 
64  assert(conn_cache == NULL);
65  conn_cache = SeqNew(100, free);
66 
68 }
69 
71 {
73 
74  for (size_t i = 0; i < SeqLength(conn_cache); i++)
75  {
76  ConnCache_entry *svp = SeqAt(conn_cache, i);
77 
78  CF_ASSERT(svp != NULL,
79  "Destroy: NULL ConnCache_entry!");
80  CF_ASSERT(svp->conn != NULL,
81  "Destroy: NULL connection in ConnCache_entry!");
82 
83  DisconnectServer(svp->conn);
84  }
85 
87  conn_cache = NULL;
88 
90 }
91 
93  const char *server,
94  const char *port,
95  ConnectionFlags flags)
96 {
97  return ConnectionFlagsEqual(&flags, &entry->conn->flags) &&
98  StringEqual(port, entry->conn->this_port) &&
99  StringEqual(server, entry->conn->this_server);
100 }
101 
103  const char *port,
104  ConnectionFlags flags)
105 {
107 
108  AgentConnection *ret_conn = NULL;
109  for (size_t i = 0; i < SeqLength(conn_cache); i++)
110  {
111  ConnCache_entry *svp = SeqAt(conn_cache, i);
112 
113  CF_ASSERT(svp != NULL,
114  "FindIdle: NULL ConnCache_entry!");
115  CF_ASSERT(svp->conn != NULL,
116  "FindIdle: NULL connection in ConnCache_entry!");
117 
118 
119  if (svp->status == CONNCACHE_STATUS_BUSY)
120  {
122  "FindIdle: connection %p seems to be busy.",
123  svp->conn);
124  }
125  else if (svp->status == CONNCACHE_STATUS_OFFLINE)
126  {
128  "FindIdle: connection %p is marked as offline.",
129  svp->conn);
130  }
131  else if (svp->status == CONNCACHE_STATUS_BROKEN)
132  {
134  "FindIdle: connection %p is marked as broken.",
135  svp->conn);
136  }
137  else if (ConnCacheEntryMatchesConnection(svp, server, port, flags))
138  {
139  if (svp->conn->conn_info->sd >= 0)
140  {
141  assert(svp->status == CONNCACHE_STATUS_IDLE);
142 
143  // Check connection state before returning it
144  int error = 0;
145  socklen_t len = sizeof(error);
146  if (getsockopt(svp->conn->conn_info->sd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
147  {
148  Log(LOG_LEVEL_DEBUG, "FindIdle: found connection to '%s' but could not get socket status, skipping.",
149  server);
151  continue;
152  }
153  if (error != 0)
154  {
155  Log(LOG_LEVEL_DEBUG, "FindIdle: found connection to '%s' but connection is broken, skipping.",
156  server);
158  continue;
159  }
160 
161  Log(LOG_LEVEL_VERBOSE, "FindIdle:"
162  " found connection to '%s' already open and ready.",
163  server);
164 
166  ret_conn = svp->conn;
167  break;
168  }
169  else
170  {
171  Log(LOG_LEVEL_VERBOSE, "FindIdle:"
172  " connection to '%s' has invalid socket descriptor %d!",
173  server, svp->conn->conn_info->sd);
175  }
176  }
177  }
178 
180 
181  if (ret_conn == NULL)
182  {
183  Log(LOG_LEVEL_VERBOSE, "FindIdle:"
184  " no existing connection to '%s' is established.", server);
185  }
186 
187  return ret_conn;
188 }
189 
191 {
192  Log(LOG_LEVEL_DEBUG, "Searching for specific busy connection to: %s",
193  conn->this_server);
194 
196 
197  bool found = false;
198  for (size_t i = 0; i < SeqLength(conn_cache); i++)
199  {
200  ConnCache_entry *svp = SeqAt(conn_cache, i);
201 
202  CF_ASSERT(svp != NULL,
203  "MarkNotBusy: NULL ConnCache_entry!");
204  CF_ASSERT(svp->conn != NULL,
205  "MarkNotBusy: NULL connection in ConnCache_entry!");
206 
207  if (svp->conn == conn)
208  {
209  /* There might be many connections to the same server, some busy
210  * some not. But here we're searching by the address of the
211  * AgentConnection object. There can be only one. */
213  "MarkNotBusy: status is not busy, it is %d!",
214  svp->status);
215 
217  found = true;
218  break;
219  }
220  }
221 
223 
224  if (!found)
225  {
226  ProgrammingError("MarkNotBusy: No busy connection found!");
227  }
228 
229  Log(LOG_LEVEL_DEBUG, "Busy connection just became free");
230 }
231 
232 /* First time we open a connection, so store it. */
234 {
235  ConnCache_entry *svp = xmalloc(sizeof(*svp));
236  svp->status = status;
237  svp->conn = conn;
238 
240  SeqAppend(conn_cache, svp);
242 }
void * xmalloc(size_t size)
Definition: alloc-mini.c:46
void free(void *)
static bool ConnectionFlagsEqual(const ConnectionFlags *f1, const ConnectionFlags *f2)
Definition: cfnet.h:63
void DisconnectServer(AgentConnection *conn)
Definition: client_code.c:309
void ConnCache_Add(AgentConnection *conn, enum ConnCacheStatus status)
Definition: conn_cache.c:233
static Seq * conn_cache
Definition: conn_cache.c:57
void ConnCache_Init()
Definition: conn_cache.c:60
void ConnCache_MarkNotBusy(AgentConnection *conn)
Definition: conn_cache.c:190
void ConnCache_Destroy()
Definition: conn_cache.c:70
static pthread_mutex_t cft_conncache
Definition: conn_cache.c:55
static bool ConnCacheEntryMatchesConnection(ConnCache_entry *entry, const char *server, const char *port, ConnectionFlags flags)
Definition: conn_cache.c:92
AgentConnection * ConnCache_FindIdleMarkBusy(const char *server, const char *port, ConnectionFlags flags)
Definition: conn_cache.c:102
ConnCacheStatus
Definition: conn_cache.h:32
@ CONNCACHE_STATUS_OFFLINE
Definition: conn_cache.h:35
@ CONNCACHE_STATUS_BUSY
Definition: conn_cache.h:34
@ CONNCACHE_STATUS_IDLE
Definition: conn_cache.h:33
@ CONNCACHE_STATUS_BROKEN
Definition: conn_cache.h:36
#define NULL
Definition: getopt1.c:56
void Log(LogLevel level, const char *fmt,...)
Definition: logging.c:409
@ LOG_LEVEL_DEBUG
Definition: logging.h:47
@ LOG_LEVEL_VERBOSE
Definition: logging.h:46
#define ProgrammingError(...)
Definition: misc_lib.h:33
#define CF_ASSERT(condition,...)
Definition: misc_lib.h:51
#define ThreadUnlock(m)
Definition: mutex.h:33
#define ThreadLock(m)
Definition: mutex.h:32
int socklen_t
Definition: platform.h:419
#define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
Definition: platform.h:427
size_t SeqLength(const Seq *seq)
Length of the sequence.
Definition: sequence.c:354
Seq * SeqNew(size_t initialCapacity, void(ItemDestroy)(void *item))
Definition: sequence.c:31
void SeqDestroy(Seq *seq)
Destroy an existing Sequence.
Definition: sequence.c:60
void SeqAppend(Seq *seq, void *item)
Append a new item to the Sequence.
Definition: sequence.c:104
static void * SeqAt(const Seq *seq, int i)
Definition: sequence.h:57
bool StringEqual(const char *const a, const char *const b)
Definition: string_lib.c:256
char * this_port
Definition: cfnet.h:113
ConnectionInfo * conn_info
Definition: cfnet.h:98
ConnectionFlags flags
Definition: cfnet.h:111
char * this_server
Definition: cfnet.h:112
Definition: conn_cache.c:49
enum ConnCacheStatus status
Definition: conn_cache.c:51
AgentConnection * conn
Definition: conn_cache.c:50
Sequence data-structure.
Definition: sequence.h:50