summaryrefslogtreecommitdiff
path: root/plugins/syslog.c
blob: 056fc5ae6a993f153e8446d539280d8aca38b297 (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
#include "system.h"

#include <stdlib.h>
#include <syslog.h>

#include <rpm/rpmstring.h>
#include <rpm/rpmts.h>
#include "lib/rpmplugin.h"

struct logstat {
    int logging;
    unsigned int scriptfail;
    unsigned int pkgfail;
};

static rpmRC syslog_init(rpmPlugin plugin, rpmts ts)
{
    /* XXX make this configurable? */
    const char * log_ident = "[RPM]";
    struct logstat * state = rcalloc(1, sizeof(*state));

    rpmPluginSetData(plugin, state);
    openlog(log_ident, (LOG_PID), LOG_USER);
    return RPMRC_OK;
}

static void syslog_cleanup(rpmPlugin plugin)
{
    struct logstat * state = rpmPluginGetData(plugin);
    free(state);
    closelog();
}

static rpmRC syslog_tsm_pre(rpmPlugin plugin, rpmts ts)
{
    struct logstat * state = rpmPluginGetData(plugin);
    
    /* Reset counters */
    state->scriptfail = 0;
    state->pkgfail = 0;

    /* Assume we are logging but... */
    state->logging = 1;

    /* ...don't log test transactions */
    if (rpmtsFlags(ts) & (RPMTRANS_FLAG_TEST|RPMTRANS_FLAG_BUILD_PROBS))
	state->logging = 0;

    /* ...don't log chroot transactions */
    if (!rstreq(rpmtsRootDir(ts), "/"))
	state->logging = 0;

    if (state->logging) {
	syslog(LOG_NOTICE, "Transaction ID %x started", rpmtsGetTid(ts));
    }

    return RPMRC_OK;
}

static rpmRC syslog_tsm_post(rpmPlugin plugin, rpmts ts, int res)
{
    struct logstat * state = rpmPluginGetData(plugin);

    if (state->logging) {
	if (state->pkgfail || state->scriptfail) {
	    syslog(LOG_WARNING, "%u elements failed, %u scripts failed",
		   state->pkgfail, state->scriptfail);
	}
	syslog(LOG_NOTICE, "Transaction ID %x finished: %d",
		rpmtsGetTid(ts), res);
    }

    state->logging = 0;
    return RPMRC_OK;
}

static rpmRC syslog_psm_post(rpmPlugin plugin, rpmte te, int res)
{
    struct logstat * state = rpmPluginGetData(plugin);

    if (state->logging) {
	int lvl = LOG_NOTICE;
	const char *op = (rpmteType(te) == TR_ADDED) ? "install" : "erase";
	const char *outcome = "success";
	/* XXX: Permit configurable header queryformat? */
	const char *pkg = rpmteNEVRA(te);

	if (res != RPMRC_OK) {
	    lvl = LOG_WARNING;
	    outcome = "failure";
	    state->pkgfail++;
	}

	syslog(lvl, "%s %s: %s", op, pkg, outcome);
    }
    return RPMRC_OK;
}

static rpmRC syslog_scriptlet_post(rpmPlugin plugin,
					const char *s_name, int type, int res)
{
    struct logstat * state = rpmPluginGetData(plugin);

    if (state->logging && res) {
	syslog(LOG_WARNING, "scriptlet %s failure: %d\n", s_name, res);
	state->scriptfail++;
    }
    return RPMRC_OK;
}

struct rpmPluginHooks_s syslog_hooks = {
    .init = syslog_init,
    .cleanup = syslog_cleanup,
    .tsm_pre = syslog_tsm_pre,
    .tsm_post = syslog_tsm_post,
    .psm_post = syslog_psm_post,
    .scriptlet_post = syslog_scriptlet_post,
};