summaryrefslogtreecommitdiffstats
path: root/src/util/file_watch.c
blob: d19fdccd608a378f3351200a62708a02fb61a529 (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Copyright (C) 2022, Red Hat Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <sys/time.h>
#include <errno.h>

#include "util/inotify.h"
#include "util/util.h"
#include "util/file_watch.h"



#define MISSING_FILE_POLL_TIME   10  /* seconds */
#define FILE_WATCH_POLL_INTERVAL  5  /* seconds */


struct file_watch_ctx {
    struct tevent_context *ev;
    const char *filename;
    bool use_inotify;

    struct config_file_inotify_check {
        struct snotify_ctx *snctx;
    } inotify_check;

    struct config_file_poll_check {
        struct tevent_timer *timer;
        time_t modified;
    } poll_check;

    fw_callback cb;
    void *cb_arg;
};


static void poll_watched_file(struct tevent_context *ev,
                             struct tevent_timer *te,
                             struct timeval t, void *ptr);

static errno_t create_poll_timer(struct file_watch_ctx *fw_ctx)
{
    struct timeval tv;

    tv = tevent_timeval_current_ofs(FILE_WATCH_POLL_INTERVAL, 0);

    fw_ctx->poll_check.timer = tevent_add_timer(fw_ctx->ev,
                                                fw_ctx,
                                                tv,
                                                poll_watched_file,
                                                fw_ctx);
    if (!fw_ctx->poll_check.timer) {
        return EIO;
    }

    return EOK;
}

static void poll_watched_file(struct tevent_context *ev,
                              struct tevent_timer *te,
                              struct timeval t, void *ptr)
{
    int ret, err;
    struct stat file_stat;
    struct file_watch_ctx *fw_ctx;

    fw_ctx = talloc_get_type(ptr, struct file_watch_ctx);

    ret = stat(fw_ctx->filename, &file_stat);
    if (ret < 0) {
        err = errno;
        DEBUG(SSSDBG_IMPORTANT_INFO,
              "Could not stat file [%s]. Error [%d:%s]\n",
              fw_ctx->filename, err, strerror(err));
        return;
    }

    if (file_stat.st_mtime != fw_ctx->poll_check.modified) {
        /* Parse the file and invoke the callback */
        /* Note: this will fire if the modification time changes into the past
         * as well as the future.
         */
        DEBUG(SSSDBG_TRACE_INTERNAL, "File [%s] changed\n", fw_ctx->filename);
        fw_ctx->poll_check.modified = file_stat.st_mtime;

        /* Tell the caller the file changed */
        fw_ctx->cb(fw_ctx->filename, fw_ctx->cb_arg);
    }

    ret = create_poll_timer(fw_ctx);
    if (ret != EOK) {
        DEBUG(SSSDBG_IMPORTANT_INFO,
              "Error: File [%s] no longer monitored for changes!\n",
              fw_ctx->filename);
    }
}


static int watched_file_inotify_cb(const char *filename,
                                  uint32_t flags,
                                  void *pvt)
{
    static char received[PATH_MAX + 1];
    static char expected[PATH_MAX + 1];
    struct file_watch_ctx *fw_ctx;
    char *res;

    DEBUG(SSSDBG_TRACE_LIBS,
          "Received inotify notification for %s\n", filename);

    fw_ctx = talloc_get_type(pvt, struct file_watch_ctx);
    if (fw_ctx == NULL) {
        return EINVAL;
    }

    res = realpath(fw_ctx->filename, expected);
    if (res == NULL) {
         DEBUG(SSSDBG_TRACE_LIBS,
               "Normalization failed for expected %s. Skipping the callback.\n",
               fw_ctx->filename);
        goto done;
    }

    res = realpath(filename, received);
    if (res == NULL) {
         DEBUG(SSSDBG_TRACE_LIBS,
               "Normalization failed for received %s. Skipping the callback.\n",
               filename);
        goto done;
    }

    if (strcmp(expected, received) == 0) {
        if (access(received, F_OK) == 0) {
            fw_ctx->cb(received, fw_ctx->cb_arg);
        } else {
            DEBUG(SSSDBG_TRACE_LIBS,
                  "File %s is missing. Skipping the callback.\n", filename);
        }
    }

done:
    return EOK;
}


static int try_inotify(struct file_watch_ctx *fw_ctx)
{
#ifdef HAVE_INOTIFY
    struct snotify_ctx *snctx;
    /* We will queue the file for update in one second.
     * This way, if there is a script writing to the file
     * repeatedly, we won't be attempting to update multiple
     * times.
     */
    struct timeval delay = { .tv_sec = 1, .tv_usec = 0 };

    snctx = snotify_create(fw_ctx, fw_ctx->ev, SNOTIFY_WATCH_DIR,
                           fw_ctx->filename, &delay,
                           IN_DELETE_SELF | IN_CLOSE_WRITE | IN_MOVE_SELF | \
                           IN_CREATE | IN_MOVED_TO | IN_IGNORED,
                           watched_file_inotify_cb, fw_ctx);
    if (snctx == NULL) {
        return EIO;
    }

    return EOK;
#else
    return EINVAL;
#endif /* HAVE_INOTIFY */
}

static errno_t fw_watch_file_poll(struct file_watch_ctx *fw_ctx)
{
    struct stat file_stat;
    int ret, err;

    ret = stat(fw_ctx->filename, &file_stat);
    if (ret < 0) {
        err = errno;
        if (err == ENOENT) {
            DEBUG(SSSDBG_IMPORTANT_INFO,
                  "File [%s] is missing. Will try again later.\n",
                  fw_ctx->filename);
        } else {
            DEBUG(SSSDBG_IMPORTANT_INFO,
                  "Could not stat file [%s]. Error [%d:%s]\n",
                  fw_ctx->filename, err, strerror(err));
        }
        return err;
    }

    fw_ctx->poll_check.modified = file_stat.st_mtime;

    if(!fw_ctx->poll_check.timer) {
        ret = create_poll_timer(fw_ctx);
        if (ret != EOK) {
            DEBUG(SSSDBG_TRACE_LIBS, "Cannot create poll timer\n");
            return ret;
        }
    }

    return EOK;
}


static int watch_file(struct file_watch_ctx *fw_ctx)
{
    int ret = EOK;
    bool use_inotify;

    use_inotify = fw_ctx->use_inotify;
    if (use_inotify) {
        ret = try_inotify(fw_ctx);
        if (ret != EOK) {
            DEBUG(SSSDBG_TRACE_LIBS, "Falling back to polling\n");
            use_inotify = false;
        }
    }

    if (!use_inotify) {
        ret = fw_watch_file_poll(fw_ctx);
    }

    return ret;
}


static void set_file_watching(struct tevent_context *ev,
                              struct tevent_timer *te,
                              struct timeval tv, void *data)
{
    int ret;
    struct file_watch_ctx *fw_ctx = talloc_get_type(data, struct file_watch_ctx);

    ret = watch_file(fw_ctx);
    if (ret == EOK) {
        if (access(fw_ctx->filename, F_OK) == 0) {
            fw_ctx->cb(fw_ctx->filename, fw_ctx->cb_arg);
        }
    } else if (ret == ENOENT) {
        DEBUG(SSSDBG_TRACE_LIBS,
              "%s missing. Waiting for it to appear.\n",
              fw_ctx->filename);
        tv = tevent_timeval_current_ofs(MISSING_FILE_POLL_TIME, 0);
        te = tevent_add_timer(fw_ctx->ev, fw_ctx, tv, set_file_watching, fw_ctx);
        if (te == NULL) {
            DEBUG(SSSDBG_IMPORTANT_INFO,
                  "tevent_add_timer failed. %s will be ignored.\n",
                  fw_ctx->filename);
        }
    } else {
        DEBUG(SSSDBG_IMPORTANT_INFO,
              "watch_file failed. %s will be ignored: [%i] %s\n",
              fw_ctx->filename, ret, sss_strerror(ret));
    }
}


struct file_watch_ctx *fw_watch_file(TALLOC_CTX *mem_ctx,
                                     struct tevent_context *ev,
                                     const char *filename,
                                     bool use_inotify,
                                     fw_callback cb,
                                     void *cb_arg)
{
    int ret;
    struct timeval tv;
    struct file_watch_ctx *fw_ctx;

    if (ev == NULL || filename == NULL || cb == NULL) {
        DEBUG(SSSDBG_TRACE_LIBS, "Invalid parameter\n");
        return NULL;
    }

    fw_ctx = talloc_zero(mem_ctx, struct file_watch_ctx);
    if (fw_ctx == NULL) {
        DEBUG(SSSDBG_IMPORTANT_INFO, "Failed to allocate the context\n");
        return NULL;
    }

    fw_ctx->ev = ev;
    fw_ctx->use_inotify = use_inotify;
    fw_ctx->cb = cb;
    fw_ctx->cb_arg = cb_arg;
    fw_ctx->filename = talloc_strdup(fw_ctx, filename);
    if (fw_ctx->filename == NULL) {
        DEBUG(SSSDBG_IMPORTANT_INFO, "talloc_strdup() failed\n");
        ret = ENOMEM;
        goto done;
    }

    /* Watch for changes to the requested file, and retry periodically
     * if the file does not exist */
    tv = tevent_timeval_current_ofs(0, 0); // Not actually used
    set_file_watching(fw_ctx->ev, NULL, tv, fw_ctx);
    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(fw_ctx);
        fw_ctx = NULL;
    }

    return fw_ctx;
}