forked from alliedtelesis/apteryx-netconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.c
189 lines (163 loc) · 5.02 KB
/
logging.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @file logging.c
* Handler of the logging configuration file
*
* Copyright 2023, Allied Telesis Labs New Zealand, Ltd
*
* 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 3 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 General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include "internal.h"
#include <sys/inotify.h>
#define READ_BUF_SIZE 512
static int inotify_fd = -1;
static GIOChannel *channel = NULL;
static char *logging_filename = NULL;
static char *logging_directory = NULL;
/* Logging flags */
int logging = LOG_NONE;
static int
load_logging_options (void)
{
FILE *fp = NULL;
gchar **split;
char *buf;
char *filename;
int count;
int i;
int flags = LOG_NONE;
int ret = 0;
buf = g_malloc0 (READ_BUF_SIZE);
filename = g_strdup_printf ("%s/%s", logging_directory, logging_filename);
fp = fopen (filename, "r");
if (fp && buf)
{
if (fgets (buf, READ_BUF_SIZE, fp) != NULL)
{
/* Remove any trailing LF */
buf[strcspn(buf, "\n")] = '\0';
split = g_strsplit (buf, " ", 6);
count = g_strv_length (split);
for (i = 0; i < count; i++)
{
if (g_strcmp0 (split[i], "edit-config") == 0)
flags |= LOG_EDIT_CONFIG;
else if (g_strcmp0 (split[i], "get") == 0)
flags |= LOG_GET;
else if (g_strcmp0 (split[i], "get-config") == 0)
flags |= LOG_GET_CONFIG;
else if (g_strcmp0 (split[i], "kill-session") == 0)
flags |= LOG_KILL_SESSION;
else if (g_strcmp0 (split[i], "lock") == 0)
flags |= LOG_LOCK;
else if (g_strcmp0 (split[i], "unlock") == 0)
flags |= LOG_UNLOCK;
}
g_strfreev (split);
}
fclose (fp);
}
else
{
ret = -1;
}
g_free (filename);
g_free (buf);
logging = flags;
return ret;
}
/**
* Handles an inotify event indicating that the logging options file may have
* been modified and reloads the logging options in the file if necessary
*/
static int
logging_file_update (void)
{
char *buf;
int total_len;
int len_read = 0;
bool file_modified = false;
int ret = -1;
/* Got a notify event informing the logging file has been modified */
if (inotify_fd >= 0)
{
buf = g_malloc0 (READ_BUF_SIZE);
total_len = read (inotify_fd, buf, READ_BUF_SIZE);
while (len_read < total_len)
{
struct inotify_event *event = (struct inotify_event *) &buf[len_read];
/* check it's the logging control file that was modified, and not
* another file in the same directory */
if (event->len &&
strncmp (event->name, logging_filename, strlen (logging_filename)) == 0)
{
file_modified = true;
}
len_read += sizeof (struct inotify_event) + event->len;
}
if (file_modified)
ret = load_logging_options ();
g_free (buf);
}
return ret;
}
static gboolean
logging_options_reload (GIOChannel *source, GIOCondition condition, gpointer data)
{
logging_file_update ();
return TRUE;
}
static int
logging_open_inotify (void)
{
if (inotify_fd < 0)
{
/* inotify doesn't tell us about creation of the logging control file
* unless we listen to inotify events for the whole directory */
inotify_fd = inotify_init ();
inotify_add_watch (inotify_fd, logging_directory,
IN_CREATE | IN_MODIFY | IN_DELETE | IN_MOVE);
}
return inotify_fd;
}
void
logging_shutdown (void)
{
if (inotify_fd >= 0)
close (inotify_fd);
if (channel)
g_io_channel_shutdown (channel, false, NULL);
if (logging_filename)
g_free (logging_filename);
}
int
logging_init (const char *path, const char *logging_arg)
{
int fd;
if (!path || !logging_arg)
return -1;
logging_directory = g_strdup (path);
logging_filename = g_strdup (logging_arg);
/* Read the current setting */
if (load_logging_options () < 0)
return -1;
/* Add an inotify watch for logging changes */
fd = logging_open_inotify ();
if (fd < 0)
return -1;
channel = g_io_channel_unix_new (fd);
g_io_add_watch (channel, G_IO_IN, logging_options_reload, NULL);
g_io_channel_unref (channel);
openlog ("netconf", LOG_PID | LOG_NDELAY, LOG_USER);
return 0;
}