File: service-management.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (168 lines) | stat: -rw-r--r-- 3,968 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2002-2013 Balabit
 * Copyright (c) 1998-2013 Balázs Scheidler
 *
 * 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 "service-management.h"
#include "messages.h"

#if SYSLOG_NG_ENABLE_SYSTEMD

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <systemd/sd-daemon.h>

#endif

typedef struct _ServiceManagement ServiceManagement;

struct _ServiceManagement
{
  ServiceManagementType type;
  void (*publish_status)(const gchar *status);
  void (*clear_status)(void);
  void (*indicate_readiness)(void);
  gboolean (*is_active)(void);
};

ServiceManagement *current_service_mgmt = NULL;

#if SYSLOG_NG_ENABLE_SYSTEMD

static inline void
service_management_systemd_publish_status(const gchar *status)
{
  gchar *status_buffer;
  time_t now = time(NULL);

  status_buffer = g_strdup_printf("STATUS=%s (%s)", status, ctime(&now));
  sd_notify(0, status_buffer);
  g_free(status_buffer);
}

static inline void
service_management_systemd_clear_status(void)
{
  sd_notify(0, "STATUS=");
}

static inline void
service_management_systemd_indicate_readiness(void)
{
  sd_notify(0, "READY=1");
}

static gboolean
service_management_systemd_is_active(void)
{
  struct stat st;

  if (lstat("/run/systemd/system/", &st) < 0 || !S_ISDIR(st.st_mode))
    {
      msg_debug("Systemd is not detected as the running init system");
      return FALSE;
    }
  else
    {
      msg_debug("Systemd is detected as the running init system");
      return TRUE;
    }
}

#endif

void
service_management_publish_status(const gchar *status)
{
  current_service_mgmt->publish_status(status);
}

void
service_management_clear_status(void)
{
  current_service_mgmt->clear_status();
}

void
service_management_indicate_readiness(void)
{
  current_service_mgmt->indicate_readiness();
}

ServiceManagementType
service_management_get_type(void)
{
  return current_service_mgmt->type;
}

static inline void
service_management_dummy_publish_status(const gchar *status)
{
}

static inline void
service_management_dummy_clear_status(void)
{
}

static inline void
service_management_dummy_indicate_readiness(void)
{
}

static gboolean
service_management_dummy_is_active(void)
{
  return TRUE;
}

ServiceManagement service_managements[] =
{
  {
    .type = SMT_NONE,
    .publish_status = service_management_dummy_publish_status,
    .clear_status = service_management_dummy_clear_status,
    .indicate_readiness = service_management_dummy_indicate_readiness,
    .is_active = service_management_dummy_is_active
  },
#if SYSLOG_NG_ENABLE_SYSTEMD
  {
    .type = SMT_SYSTEMD,
    .publish_status = service_management_systemd_publish_status,
    .clear_status = service_management_systemd_clear_status,
    .indicate_readiness = service_management_systemd_indicate_readiness,
    .is_active = service_management_systemd_is_active
  }
#endif
};

void
service_management_init(void)
{
  gint i = 0;
  while (i < sizeof(service_managements) / sizeof(ServiceManagement))
    {
      if (service_managements[i].is_active())
        current_service_mgmt = &service_managements[i];
      i++;
    }
}