~ubuntu-branches/ubuntu/trusty/pacemaker/trusty-proposed

« back to all changes in this revision

Viewing changes to .pc/Fix-lrmd-Prevent-glib-timers-removed-more-than-once.patch/lib/services/services_linux.c

  • Committer: Package Import Robot
  • Author(s): Rafael David Tinoco, James Page, Rafael David Tinoco
  • Date: 2014-12-18 10:09:39 UTC
  • Revision ID: package-import@ubuntu.com-20141218100939-d38bc1vv5220nmu5
Tags: 1.1.10+git20130802-1ubuntu2.2
[ James Page ]
* d/control: Ensure that pacemaker binary package uses matched binary
  versions of pacemaker libraries, avoiding upgrade problems (LP: #1382842).

[ Rafael David Tinoco ]
* d/p/Fix-lrmd-Prevent-glib-timers-removed-more-than-once.patch:
  Prevent glib assert triggered by timers being removed from mainloop more
  than once, cherry picked from upstream VCS (LP: #1368737).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Andrew Beekhof <andrew@beekhof.net>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2.1 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This software is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public
 
15
 * License along with this library; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 */
 
18
 
 
19
#include <crm_internal.h>
 
20
 
 
21
#ifndef _GNU_SOURCE
 
22
#  define _GNU_SOURCE
 
23
#endif
 
24
 
 
25
#include <sys/types.h>
 
26
#include <sys/stat.h>
 
27
#include <sys/wait.h>
 
28
#include <errno.h>
 
29
#include <unistd.h>
 
30
#include <dirent.h>
 
31
#include <fcntl.h>
 
32
#include <string.h>
 
33
 
 
34
#ifdef HAVE_SYS_SIGNALFD_H
 
35
#include <sys/signalfd.h>
 
36
#endif
 
37
 
 
38
#include "crm/crm.h"
 
39
#include "crm/common/mainloop.h"
 
40
#include "crm/services.h"
 
41
 
 
42
#include "services_private.h"
 
43
 
 
44
#if SUPPORT_CIBSECRETS
 
45
#  include "crm/common/cib_secrets.h"
 
46
#endif
 
47
 
 
48
static inline void
 
49
set_fd_opts(int fd, int opts)
 
50
{
 
51
    int flag;
 
52
 
 
53
    if ((flag = fcntl(fd, F_GETFL)) >= 0) {
 
54
        if (fcntl(fd, F_SETFL, flag | opts) < 0) {
 
55
            crm_err("fcntl() write failed");
 
56
        }
 
57
    } else {
 
58
        crm_err("fcntl() read failed");
 
59
    }
 
60
}
 
61
 
 
62
static gboolean
 
63
read_output(int fd, svc_action_t * op)
 
64
{
 
65
    char *data = NULL;
 
66
    int rc = 0, len = 0;
 
67
    gboolean is_err = FALSE;
 
68
    char buf[500];
 
69
    static const size_t buf_read_len = sizeof(buf) - 1;
 
70
 
 
71
    crm_trace("%p", op);
 
72
 
 
73
    if (fd < 0) {
 
74
        return FALSE;
 
75
    }
 
76
 
 
77
    if (fd == op->opaque->stderr_fd) {
 
78
        is_err = TRUE;
 
79
        if (op->stderr_data) {
 
80
            len = strlen(op->stderr_data);
 
81
            data = op->stderr_data;
 
82
        }
 
83
    } else if (op->stdout_data) {
 
84
        len = strlen(op->stdout_data);
 
85
        data = op->stdout_data;
 
86
    }
 
87
 
 
88
    do {
 
89
        rc = read(fd, buf, buf_read_len);
 
90
        if (rc > 0) {
 
91
            buf[rc] = 0;
 
92
            data = realloc(data, len + rc + 1);
 
93
            sprintf(data + len, "%s", buf);
 
94
            len += rc;
 
95
        } else if (errno != EINTR) {
 
96
            /* error or EOF
 
97
             * Cleanup happens in pipe_done()
 
98
             */
 
99
            rc = FALSE;
 
100
            break;
 
101
        }
 
102
 
 
103
    } while (rc == buf_read_len || rc < 0);
 
104
 
 
105
    if (data != NULL && is_err) {
 
106
        op->stderr_data = data;
 
107
    } else if (data != NULL) {
 
108
        op->stdout_data = data;
 
109
    }
 
110
 
 
111
    return rc;
 
112
}
 
113
 
 
114
static int
 
115
dispatch_stdout(gpointer userdata)
 
116
{
 
117
    svc_action_t *op = (svc_action_t *) userdata;
 
118
 
 
119
    return read_output(op->opaque->stdout_fd, op);
 
120
}
 
121
 
 
122
static int
 
123
dispatch_stderr(gpointer userdata)
 
124
{
 
125
    svc_action_t *op = (svc_action_t *) userdata;
 
126
 
 
127
    return read_output(op->opaque->stderr_fd, op);
 
128
}
 
129
 
 
130
static void
 
131
pipe_out_done(gpointer user_data)
 
132
{
 
133
    svc_action_t *op = (svc_action_t *) user_data;
 
134
 
 
135
    crm_trace("%p", op);
 
136
 
 
137
    op->opaque->stdout_gsource = NULL;
 
138
    if (op->opaque->stdout_fd > STDOUT_FILENO) {
 
139
        close(op->opaque->stdout_fd);
 
140
    }
 
141
    op->opaque->stdout_fd = -1;
 
142
}
 
143
 
 
144
static void
 
145
pipe_err_done(gpointer user_data)
 
146
{
 
147
    svc_action_t *op = (svc_action_t *) user_data;
 
148
 
 
149
    op->opaque->stderr_gsource = NULL;
 
150
    if (op->opaque->stderr_fd > STDERR_FILENO) {
 
151
        close(op->opaque->stderr_fd);
 
152
    }
 
153
    op->opaque->stderr_fd = -1;
 
154
}
 
155
 
 
156
static struct mainloop_fd_callbacks stdout_callbacks = {
 
157
    .dispatch = dispatch_stdout,
 
158
    .destroy = pipe_out_done,
 
159
};
 
160
 
 
161
static struct mainloop_fd_callbacks stderr_callbacks = {
 
162
    .dispatch = dispatch_stderr,
 
163
    .destroy = pipe_err_done,
 
164
};
 
165
 
 
166
static void
 
167
set_ocf_env(const char *key, const char *value, gpointer user_data)
 
168
{
 
169
    if (setenv(key, value, 1) != 0) {
 
170
        crm_perror(LOG_ERR, "setenv failed for key:%s and value:%s", key, value);
 
171
    }
 
172
}
 
173
 
 
174
static void
 
175
set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
 
176
{
 
177
    char buffer[500];
 
178
 
 
179
    snprintf(buffer, sizeof(buffer), "OCF_RESKEY_%s", (char *)key);
 
180
    set_ocf_env(buffer, value, user_data);
 
181
}
 
182
 
 
183
static void
 
184
add_OCF_env_vars(svc_action_t * op)
 
185
{
 
186
    if (!op->standard || strcasecmp("ocf", op->standard) != 0) {
 
187
        return;
 
188
    }
 
189
 
 
190
    if (op->params) {
 
191
        g_hash_table_foreach(op->params, set_ocf_env_with_prefix, NULL);
 
192
    }
 
193
 
 
194
    set_ocf_env("OCF_RA_VERSION_MAJOR", "1", NULL);
 
195
    set_ocf_env("OCF_RA_VERSION_MINOR", "0", NULL);
 
196
    set_ocf_env("OCF_ROOT", OCF_ROOT_DIR, NULL);
 
197
 
 
198
    if (op->rsc) {
 
199
        set_ocf_env("OCF_RESOURCE_INSTANCE", op->rsc, NULL);
 
200
    }
 
201
 
 
202
    if (op->agent != NULL) {
 
203
        set_ocf_env("OCF_RESOURCE_TYPE", op->agent, NULL);
 
204
    }
 
205
 
 
206
    /* Notes: this is not added to specification yet. Sept 10,2004 */
 
207
    if (op->provider != NULL) {
 
208
        set_ocf_env("OCF_RESOURCE_PROVIDER", op->provider, NULL);
 
209
    }
 
210
}
 
211
 
 
212
gboolean
 
213
recurring_action_timer(gpointer data)
 
214
{
 
215
    svc_action_t *op = data;
 
216
 
 
217
    crm_debug("Scheduling another invokation of %s", op->id);
 
218
 
 
219
    /* Clean out the old result */
 
220
    free(op->stdout_data);
 
221
    op->stdout_data = NULL;
 
222
    free(op->stderr_data);
 
223
    op->stderr_data = NULL;
 
224
 
 
225
    services_action_async(op, NULL);
 
226
    return FALSE;
 
227
}
 
228
 
 
229
void
 
230
operation_finalize(svc_action_t * op)
 
231
{
 
232
    int recurring = 0;
 
233
 
 
234
    if (op->interval) {
 
235
        if (op->cancel) {
 
236
            op->status = PCMK_LRM_OP_CANCELLED;
 
237
            cancel_recurring_action(op);
 
238
        } else {
 
239
            recurring = 1;
 
240
            op->opaque->repeat_timer = g_timeout_add(op->interval,
 
241
                                                     recurring_action_timer, (void *)op);
 
242
        }
 
243
    }
 
244
 
 
245
    if (op->opaque->callback) {
 
246
        op->opaque->callback(op);
 
247
    }
 
248
 
 
249
    op->pid = 0;
 
250
 
 
251
    if (!recurring) {
 
252
        /*
 
253
         * If this is a recurring action, do not free explicitly.
 
254
         * It will get freed whenever the action gets cancelled.
 
255
         */
 
256
        services_action_free(op);
 
257
    }
 
258
}
 
259
 
 
260
static void
 
261
operation_finished(mainloop_child_t * p, pid_t pid, int core, int signo, int exitcode)
 
262
{
 
263
    svc_action_t *op = mainloop_child_userdata(p);
 
264
    char *prefix = g_strdup_printf("%s:%d", op->id, op->pid);
 
265
 
 
266
    mainloop_clear_child_userdata(p);
 
267
    op->status = PCMK_LRM_OP_DONE;
 
268
    CRM_ASSERT(op->pid == pid);
 
269
 
 
270
    if (op->opaque->stderr_gsource) {
 
271
        /* Make sure we have read everything from the buffer.
 
272
         * Depending on the priority mainloop gives the fd, operation_finished
 
273
         * could occur before all the reads are done.  Force the read now.*/
 
274
        dispatch_stderr(op);
 
275
    }
 
276
 
 
277
    if (op->opaque->stdout_gsource) {
 
278
        /* Make sure we have read everything from the buffer.
 
279
         * Depending on the priority mainloop gives the fd, operation_finished
 
280
         * could occur before all the reads are done.  Force the read now.*/
 
281
        dispatch_stdout(op);
 
282
    }
 
283
 
 
284
    if (signo) {
 
285
        if (mainloop_child_timeout(p)) {
 
286
            crm_warn("%s - timed out after %dms", prefix, op->timeout);
 
287
            op->status = PCMK_LRM_OP_TIMEOUT;
 
288
            op->rc = PCMK_OCF_TIMEOUT;
 
289
 
 
290
        } else {
 
291
            crm_warn("%s - terminated with signal %d", prefix, signo);
 
292
            op->status = PCMK_LRM_OP_ERROR;
 
293
            op->rc = PCMK_OCF_SIGNAL;
 
294
        }
 
295
 
 
296
    } else {
 
297
        op->rc = exitcode;
 
298
        crm_debug("%s - exited with rc=%d", prefix, exitcode);
 
299
    }
 
300
 
 
301
    g_free(prefix);
 
302
    prefix = g_strdup_printf("%s:%d:stderr", op->id, op->pid);
 
303
    crm_log_output(LOG_NOTICE, prefix, op->stderr_data);
 
304
 
 
305
    g_free(prefix);
 
306
    prefix = g_strdup_printf("%s:%d:stdout", op->id, op->pid);
 
307
    crm_log_output(LOG_DEBUG, prefix, op->stdout_data);
 
308
 
 
309
    g_free(prefix);
 
310
    operation_finalize(op);
 
311
}
 
312
 
 
313
gboolean
 
314
services_os_action_execute(svc_action_t * op, gboolean synchronous)
 
315
{
 
316
    int rc, lpc;
 
317
    int stdout_fd[2];
 
318
    int stderr_fd[2];
 
319
    sigset_t mask;
 
320
    sigset_t old_mask;
 
321
 
 
322
    if (pipe(stdout_fd) < 0) {
 
323
        crm_err("pipe() failed");
 
324
    }
 
325
 
 
326
    if (pipe(stderr_fd) < 0) {
 
327
        crm_err("pipe() failed");
 
328
    }
 
329
 
 
330
    if (synchronous) {
 
331
        sigemptyset(&mask);
 
332
        sigaddset(&mask, SIGCHLD);
 
333
        sigemptyset(&old_mask);
 
334
 
 
335
        if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) {
 
336
            crm_perror(LOG_ERR, "sigprocmask() failed");
 
337
        }
 
338
    }
 
339
 
 
340
    op->pid = fork();
 
341
    switch (op->pid) {
 
342
        case -1:
 
343
            crm_err("fork() failed");
 
344
            close(stdout_fd[0]);
 
345
            close(stdout_fd[1]);
 
346
            close(stderr_fd[0]);
 
347
            close(stderr_fd[1]);
 
348
            return FALSE;
 
349
 
 
350
        case 0:                /* Child */
 
351
            /* Man: The call setpgrp() is equivalent to setpgid(0,0)
 
352
             * _and_ compiles on BSD variants too
 
353
             * need to investigate if it works the same too.
 
354
             */
 
355
            setpgid(0, 0);
 
356
            close(stdout_fd[0]);
 
357
            close(stderr_fd[0]);
 
358
            if (STDOUT_FILENO != stdout_fd[1]) {
 
359
                if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
 
360
                    crm_err("dup2() failed (stdout)");
 
361
                }
 
362
                close(stdout_fd[1]);
 
363
            }
 
364
            if (STDERR_FILENO != stderr_fd[1]) {
 
365
                if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
 
366
                    crm_err("dup2() failed (stderr)");
 
367
                }
 
368
                close(stderr_fd[1]);
 
369
            }
 
370
 
 
371
            /* close all descriptors except stdin/out/err and channels to logd */
 
372
            for (lpc = getdtablesize() - 1; lpc > STDERR_FILENO; lpc--) {
 
373
                close(lpc);
 
374
            }
 
375
 
 
376
#if SUPPORT_CIBSECRETS
 
377
            if (replace_secret_params(op->rsc, op->params) < 0) {
 
378
                /* replacing secrets failed! */
 
379
                if (safe_str_eq(op->action,"stop")) {
 
380
                    /* don't fail on stop! */
 
381
                    crm_info("proceeding with the stop operation for %s", op->rsc);
 
382
 
 
383
                } else {
 
384
                    crm_err("failed to get secrets for %s, "
 
385
                            "considering resource not configured", op->rsc);
 
386
                    _exit(PCMK_OCF_NOT_CONFIGURED);
 
387
                }
 
388
            }
 
389
#endif
 
390
            /* Setup environment correctly */
 
391
            add_OCF_env_vars(op);
 
392
 
 
393
            /* execute the RA */
 
394
            execvp(op->opaque->exec, op->opaque->args);
 
395
 
 
396
            switch (errno) {    /* see execve(2) */
 
397
                case ENOENT:   /* No such file or directory */
 
398
                case EISDIR:   /* Is a directory */
 
399
                    rc = PCMK_OCF_NOT_INSTALLED;
 
400
#if SUPPORT_NAGIOS
 
401
                    if (safe_str_eq(op->standard, "nagios")) {
 
402
                        rc = NAGIOS_NOT_INSTALLED;
 
403
                    }
 
404
#endif
 
405
                    break;
 
406
                case EACCES:   /* permission denied (various errors) */
 
407
                    rc = PCMK_OCF_INSUFFICIENT_PRIV;
 
408
#if SUPPORT_NAGIOS
 
409
                    if (safe_str_eq(op->standard, "nagios")) {
 
410
                        rc = NAGIOS_INSUFFICIENT_PRIV;
 
411
                    }
 
412
#endif
 
413
                    break;
 
414
                default:
 
415
                    rc = PCMK_OCF_UNKNOWN_ERROR;
 
416
                    break;
 
417
            }
 
418
            _exit(rc);
 
419
    }
 
420
 
 
421
    /* Only the parent reaches here */
 
422
    close(stdout_fd[1]);
 
423
    close(stderr_fd[1]);
 
424
 
 
425
    op->opaque->stdout_fd = stdout_fd[0];
 
426
    set_fd_opts(op->opaque->stdout_fd, O_NONBLOCK);
 
427
 
 
428
    op->opaque->stderr_fd = stderr_fd[0];
 
429
    set_fd_opts(op->opaque->stderr_fd, O_NONBLOCK);
 
430
 
 
431
    if (synchronous) {
 
432
#ifndef HAVE_SYS_SIGNALFD_H
 
433
        CRM_ASSERT(FALSE);
 
434
#else
 
435
        int status = 0;
 
436
        int timeout = op->timeout;
 
437
        int sfd = -1;
 
438
        time_t start = -1;
 
439
        struct pollfd fds[3];
 
440
        int wait_rc = 0;
 
441
 
 
442
        sfd = signalfd(-1, &mask, 0);
 
443
        if (sfd < 0) {
 
444
            crm_perror(LOG_ERR, "signalfd() failed");
 
445
        }
 
446
 
 
447
        fds[0].fd = op->opaque->stdout_fd;
 
448
        fds[0].events = POLLIN;
 
449
        fds[0].revents = 0;
 
450
 
 
451
        fds[1].fd = op->opaque->stderr_fd;
 
452
        fds[1].events = POLLIN;
 
453
        fds[1].revents = 0;
 
454
 
 
455
        fds[2].fd = sfd;
 
456
        fds[2].events = POLLIN;
 
457
        fds[2].revents = 0;
 
458
 
 
459
        crm_trace("Waiting for %d", op->pid);
 
460
        start = time(NULL);
 
461
        do {
 
462
            int poll_rc = poll(fds, 3, timeout);
 
463
 
 
464
            if (poll_rc > 0) {
 
465
                if (fds[0].revents & POLLIN) {
 
466
                    read_output(op->opaque->stdout_fd, op);
 
467
                }
 
468
 
 
469
                if (fds[1].revents & POLLIN) {
 
470
                    read_output(op->opaque->stderr_fd, op);
 
471
                }
 
472
 
 
473
                if (fds[2].revents & POLLIN) {
 
474
                    struct signalfd_siginfo fdsi;
 
475
                    ssize_t s;
 
476
 
 
477
                    s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
 
478
                    if (s != sizeof(struct signalfd_siginfo)) {
 
479
                        crm_perror(LOG_ERR, "Read from signal fd %d failed", sfd);
 
480
 
 
481
                    } else if (fdsi.ssi_signo == SIGCHLD) {
 
482
                        wait_rc = waitpid(op->pid, &status, WNOHANG);
 
483
 
 
484
                        if (wait_rc < 0){
 
485
                            crm_perror(LOG_ERR, "waitpid() for %d failed", op->pid);
 
486
 
 
487
                        } else if (wait_rc > 0) {
 
488
                            break;
 
489
                        }
 
490
                    }
 
491
                }
 
492
 
 
493
            } else if (poll_rc == 0) {
 
494
                timeout = 0;
 
495
                break;
 
496
 
 
497
            } else if (poll_rc < 0) {
 
498
                if (errno != EINTR) {
 
499
                    crm_perror(LOG_ERR, "poll() failed");
 
500
                    break;
 
501
                }
 
502
            }
 
503
 
 
504
            timeout = op->timeout - (time(NULL) - start) * 1000;
 
505
 
 
506
        } while ((op->timeout < 0 || timeout > 0));
 
507
 
 
508
        crm_trace("Child done: %d", op->pid);
 
509
        if (wait_rc <= 0) {
 
510
            int killrc = kill(op->pid, SIGKILL);
 
511
 
 
512
            op->rc = PCMK_OCF_UNKNOWN_ERROR;
 
513
            if (op->timeout > 0 && timeout <= 0) {
 
514
                op->status = PCMK_LRM_OP_TIMEOUT;
 
515
                crm_warn("%s:%d - timed out after %dms", op->id, op->pid, op->timeout);
 
516
 
 
517
            } else {
 
518
                op->status = PCMK_LRM_OP_ERROR;
 
519
            }
 
520
 
 
521
            if (killrc && errno != ESRCH) {
 
522
                crm_err("kill(%d, KILL) failed: %d", op->pid, errno);
 
523
            }
 
524
            /*
 
525
             * From sigprocmask(2):
 
526
             * It is not possible to block SIGKILL or SIGSTOP.  Attempts to do so are silently ignored.
 
527
             *
 
528
             * This makes it safe to skip WNOHANG here
 
529
             */
 
530
            waitpid(op->pid, &status, 0);
 
531
 
 
532
        } else if (WIFEXITED(status)) {
 
533
            op->status = PCMK_LRM_OP_DONE;
 
534
            op->rc = WEXITSTATUS(status);
 
535
            crm_info("Managed %s process %d exited with rc=%d", op->id, op->pid, op->rc);
 
536
 
 
537
        } else if (WIFSIGNALED(status)) {
 
538
            int signo = WTERMSIG(status);
 
539
 
 
540
            op->status = PCMK_LRM_OP_ERROR;
 
541
            crm_err("Managed %s process %d exited with signal=%d", op->id, op->pid, signo);
 
542
        }
 
543
#ifdef WCOREDUMP
 
544
        if (WCOREDUMP(status)) {
 
545
            crm_err("Managed %s process %d dumped core", op->id, op->pid);
 
546
        }
 
547
#endif
 
548
 
 
549
        read_output(op->opaque->stdout_fd, op);
 
550
        read_output(op->opaque->stderr_fd, op);
 
551
 
 
552
        close(op->opaque->stdout_fd);
 
553
        close(op->opaque->stderr_fd);
 
554
        close(sfd);
 
555
 
 
556
        if (sigismember(&old_mask, SIGCHLD) == 0) {
 
557
            if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) {
 
558
                crm_perror(LOG_ERR, "sigprocmask() to unblocked failed");
 
559
            }
 
560
        }
 
561
#endif
 
562
 
 
563
    } else {
 
564
        crm_trace("Async waiting for %d - %s", op->pid, op->opaque->exec);
 
565
        mainloop_child_add(op->pid, op->timeout, op->id, op, operation_finished);
 
566
 
 
567
        op->opaque->stdout_gsource = mainloop_add_fd(op->id,
 
568
                                                     G_PRIORITY_LOW,
 
569
                                                     op->opaque->stdout_fd, op, &stdout_callbacks);
 
570
 
 
571
        op->opaque->stderr_gsource = mainloop_add_fd(op->id,
 
572
                                                     G_PRIORITY_LOW,
 
573
                                                     op->opaque->stderr_fd, op, &stderr_callbacks);
 
574
    }
 
575
 
 
576
    return TRUE;
 
577
}
 
578
 
 
579
GList *
 
580
services_os_get_directory_list(const char *root, gboolean files, gboolean executable)
 
581
{
 
582
    GList *list = NULL;
 
583
    struct dirent **namelist;
 
584
    int entries = 0, lpc = 0;
 
585
    char buffer[PATH_MAX];
 
586
 
 
587
    entries = scandir(root, &namelist, NULL, alphasort);
 
588
    if (entries <= 0) {
 
589
        return list;
 
590
    }
 
591
 
 
592
    for (lpc = 0; lpc < entries; lpc++) {
 
593
        struct stat sb;
 
594
 
 
595
        if ('.' == namelist[lpc]->d_name[0]) {
 
596
            free(namelist[lpc]);
 
597
            continue;
 
598
        }
 
599
 
 
600
        snprintf(buffer, sizeof(buffer), "%s/%s", root, namelist[lpc]->d_name);
 
601
 
 
602
        if (stat(buffer, &sb)) {
 
603
            continue;
 
604
        }
 
605
 
 
606
        if (S_ISDIR(sb.st_mode)) {
 
607
            if (files) {
 
608
                free(namelist[lpc]);
 
609
                continue;
 
610
            }
 
611
 
 
612
        } else if (S_ISREG(sb.st_mode)) {
 
613
            if (files == FALSE) {
 
614
                free(namelist[lpc]);
 
615
                continue;
 
616
 
 
617
            } else if (executable
 
618
                       && (sb.st_mode & S_IXUSR) == 0
 
619
                       && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
 
620
                free(namelist[lpc]);
 
621
                continue;
 
622
            }
 
623
        }
 
624
 
 
625
        list = g_list_append(list, strdup(namelist[lpc]->d_name));
 
626
 
 
627
        free(namelist[lpc]);
 
628
    }
 
629
 
 
630
    free(namelist);
 
631
    return list;
 
632
}
 
633
 
 
634
GList *
 
635
resources_os_list_lsb_agents(void)
 
636
{
 
637
    return get_directory_list(LSB_ROOT_DIR, TRUE, TRUE);
 
638
}
 
639
 
 
640
GList *
 
641
resources_os_list_ocf_providers(void)
 
642
{
 
643
    return get_directory_list(OCF_ROOT_DIR "/resource.d", FALSE, TRUE);
 
644
}
 
645
 
 
646
GList *
 
647
resources_os_list_ocf_agents(const char *provider)
 
648
{
 
649
    GList *gIter = NULL;
 
650
    GList *result = NULL;
 
651
    GList *providers = NULL;
 
652
 
 
653
    if (provider) {
 
654
        char buffer[500];
 
655
 
 
656
        snprintf(buffer, sizeof(buffer), "%s/resource.d/%s", OCF_ROOT_DIR, provider);
 
657
        return get_directory_list(buffer, TRUE, TRUE);
 
658
    }
 
659
 
 
660
    providers = resources_os_list_ocf_providers();
 
661
    for (gIter = providers; gIter != NULL; gIter = gIter->next) {
 
662
        GList *tmp1 = result;
 
663
        GList *tmp2 = resources_os_list_ocf_agents(gIter->data);
 
664
 
 
665
        if (tmp2) {
 
666
            result = g_list_concat(tmp1, tmp2);
 
667
        }
 
668
    }
 
669
    g_list_free_full(providers, free);
 
670
    return result;
 
671
}
 
672
 
 
673
#if SUPPORT_NAGIOS
 
674
GList *
 
675
resources_os_list_nagios_agents(void)
 
676
{
 
677
    GList *plugin_list = NULL;
 
678
    GList *result = NULL;
 
679
    GList *gIter = NULL;
 
680
 
 
681
    plugin_list = get_directory_list(NAGIOS_PLUGIN_DIR, TRUE, TRUE);
 
682
 
 
683
    /* Make sure both the plugin and its metadata exist */
 
684
    for (gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
 
685
        const char *plugin = gIter->data;
 
686
        char *metadata = g_strdup_printf(NAGIOS_METADATA_DIR "/%s.xml", plugin);
 
687
        struct stat st;
 
688
 
 
689
        if (stat(metadata, &st) == 0) {
 
690
            result = g_list_append(result, strdup(plugin));
 
691
        }
 
692
 
 
693
        g_free(metadata);
 
694
    }
 
695
    g_list_free_full(plugin_list, free);
 
696
    return result;
 
697
}
 
698
#endif