suricata
tm-queues.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Thread module management functions
24  */
25 
26 #include "suricata.h"
27 #include "threads.h"
28 #include "tm-queues.h"
29 #include "util-debug.h"
30 
31 #define TMQ_MAX_QUEUES 256
32 
33 static uint16_t tmq_id = 0;
34 static Tmq tmqs[TMQ_MAX_QUEUES];
35 
36 Tmq *TmqCreateQueue(const char *name)
37 {
38  if (tmq_id >= TMQ_MAX_QUEUES)
39  goto error;
40 
41  Tmq *q = &tmqs[tmq_id];
42  q->name = SCStrdup(name);
43  if (q->name == NULL)
44  goto error;
45 
46  q->id = tmq_id++;
47 
48  SCLogDebug("created queue \'%s\', %p", name, q);
49  return q;
50 
51 error:
52  SCLogError(SC_ERR_THREAD_QUEUE, "too many thread queues %u, max is %u", tmq_id+1, TMQ_MAX_QUEUES);
53  return NULL;
54 }
55 
56 Tmq* TmqGetQueueByName(const char *name)
57 {
58  uint16_t i;
59 
60  for (i = 0; i < tmq_id; i++) {
61  if (strcmp(tmqs[i].name, name) == 0)
62  return &tmqs[i];
63  }
64 
65  return NULL;
66 }
67 
68 void TmqDebugList(void)
69 {
70  uint16_t i = 0;
71  for (i = 0; i < tmq_id; i++) {
72  /* get a lock accessing the len */
73  SCMutexLock(&trans_q[tmqs[i].id].mutex_q);
74  printf("TmqDebugList: id %" PRIu32 ", name \'%s\', len %" PRIu32 "\n", tmqs[i].id, tmqs[i].name, trans_q[tmqs[i].id].len);
75  SCMutexUnlock(&trans_q[tmqs[i].id].mutex_q);
76  }
77 }
78 
79 void TmqResetQueues(void)
80 {
81  uint16_t i;
82  for (i = 0; i < TMQ_MAX_QUEUES; i++) {
83  if (tmqs[i].name) {
84  SCFree(tmqs[i].name);
85  }
86  }
87  memset(&tmqs, 0x00, sizeof(tmqs));
88  tmq_id = 0;
89 }
90 
91 /**
92  * \brief Checks if all the queues allocated so far have at least one reader
93  * and writer.
94  */
96 {
97  int i = 0;
98  char err = FALSE;
99 
100  for (i = 0; i < tmq_id; i++) {
101  SCMutexLock(&trans_q[tmqs[i].id].mutex_q);
102  if (tmqs[i].reader_cnt == 0) {
103  SCLogError(SC_ERR_THREAD_QUEUE, "queue \"%s\" doesn't have a reader (id %d, max %u)", tmqs[i].name, i, tmq_id);
104  err = TRUE;
105  } else if (tmqs[i].writer_cnt == 0) {
106  SCLogError(SC_ERR_THREAD_QUEUE, "queue \"%s\" doesn't have a writer (id %d, max %u)", tmqs[i].name, i, tmq_id);
107  err = TRUE;
108  }
109  SCMutexUnlock(&trans_q[tmqs[i].id].mutex_q);
110 
111  if (err == TRUE)
112  goto error;
113  }
114 
115  return;
116 
117 error:
118  SCLogError(SC_ERR_FATAL, "fatal error during threading setup");
119  exit(EXIT_FAILURE);
120 }
Tmq * TmqGetQueueByName(const char *name)
Definition: tm-queues.c:56
void TmqResetQueues(void)
Definition: tm-queues.c:79
#define SCLogDebug(...)
Definition: util-debug.h:335
#define FALSE
#define TMQ_MAX_QUEUES
Definition: tm-queues.c:31
uint16_t id
Definition: tm-queues.h:29
#define TRUE
#define SCMutexLock(mut)
#define SCMutexUnlock(mut)
Definition: tm-queues.h:27
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void TmValidateQueueState(void)
Checks if all the queues allocated so far have at least one reader and writer.
Definition: tm-queues.c:95
#define SCFree(a)
Definition: util-mem.h:322
PacketQueue trans_q[256]
Definition: suricata.h:132
#define SCStrdup(a)
Definition: util-mem.h:268
uint8_t len
void TmqDebugList(void)
Definition: tm-queues.c:68
char * name
Definition: tm-queues.h:28
Tmq * TmqCreateQueue(const char *name)
Definition: tm-queues.c:36