File: control-command-thread.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (172 lines) | stat: -rw-r--r-- 4,759 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (c) 2002-2013 Balabit
 * Copyright (c) 2021 Balazs Scheidler <bazsi77@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "control-command-thread.h"
#include "control-connection.h"
#include "control-server.h"
#include "messages.h"
#include "secret-storage/secret-storage.h"
#include "scratch-buffers.h"
#include <iv_event.h>

struct _ControlCommandThread
{
  GAtomicCounter ref_cnt;
  ControlConnection *connection;
  GString *command;
  gpointer user_data;

  GThread *thread;
  GMutex state_lock;
  gboolean cancelled;
  gboolean finished;
  ControlCommandFunc func;
  struct iv_event thread_finished;
};

static void
_on_thread_finished(gpointer user_data)
{
  ControlCommandThread *self = (ControlCommandThread *) user_data;

  g_thread_join(self->thread);
  iv_event_unregister(&self->thread_finished);

  control_server_worker_finished(self->connection->server, self);

  /* drop ref belonging to the thread_finished event */
  control_command_thread_unref(self);
}

static void
_thread(gpointer user_data)
{
  ControlCommandThread *self = (ControlCommandThread *) user_data;

  iv_init();
  scratch_buffers_allocator_init();

  msg_debug("Control command thread has started",
            evt_tag_str("control_command", self->command->str));
  self->func(self->connection, self->command, self->user_data, &self->cancelled);
  g_mutex_lock(&self->state_lock);
  {
    self->finished = TRUE;
    if (!self->cancelled)
      iv_event_post(&self->thread_finished);
  }
  g_mutex_unlock(&self->state_lock);

  /* drop ref belonging to the thread function */
  msg_debug("Control command thread is exiting now",
            evt_tag_str("control_command", self->command->str));

  scratch_buffers_explicit_gc();
  scratch_buffers_allocator_deinit();
  control_command_thread_unref(self);
  iv_deinit();
}

const gchar *
control_command_thread_get_command(ControlCommandThread *self)
{
  return self->command->str;
}

void
control_command_thread_run(ControlCommandThread *self)
{
  self->thread_finished.cookie = control_command_thread_ref(self);
  iv_event_register(&self->thread_finished);

  control_server_worker_started(self->connection->server, self);
  self->thread = g_thread_new(self->command->str, (GThreadFunc) _thread, control_command_thread_ref(self));
}

void
control_command_thread_cancel(ControlCommandThread *self)
{
  gboolean has_to_join = FALSE;

  g_mutex_lock(&self->state_lock);
  {
    self->cancelled = TRUE;
    if (!self->finished)
      {
        has_to_join = TRUE;
      }
  }
  g_mutex_unlock(&self->state_lock);

  if (has_to_join)
    g_thread_join(self->thread);
}

ControlCommandThread *
control_command_thread_new(ControlConnection *cc, GString *cmd, ControlCommandFunc func, gpointer user_data)
{
  ControlCommandThread *self = g_new0(ControlCommandThread, 1);

  g_atomic_counter_set(&self->ref_cnt, 1);
  self->connection = control_connection_ref(cc);
  self->command = g_string_new(cmd->str);
  self->func = func;
  self->user_data = user_data;

  g_mutex_init(&self->state_lock);

  IV_EVENT_INIT(&self->thread_finished);
  self->thread_finished.handler = _on_thread_finished;

  return self;
}

static void
_control_command_thread_free(ControlCommandThread *self)
{
  g_mutex_clear(&self->state_lock);
  secret_storage_wipe(self->command->str, self->command->len);
  g_string_free(self->command, TRUE);
  control_connection_unref(self->connection);
  g_free(self);
}

ControlCommandThread *
control_command_thread_ref(ControlCommandThread *self)
{
  g_assert(!self || g_atomic_counter_get(&self->ref_cnt) > 0);

  if (self)
    g_atomic_counter_inc(&self->ref_cnt);

  return self;
}

void
control_command_thread_unref(ControlCommandThread *self)
{
  g_assert(!self || g_atomic_counter_get(&self->ref_cnt));

  if (self && (g_atomic_counter_dec_and_test(&self->ref_cnt)))
    _control_command_thread_free(self);
}