upgrade rpm version to 4.14.1
[platform/upstream/rpm.git] / plugins / syslog.c
1 #include "system.h"
2
3 #include <syslog.h>
4
5 #include <rpm/rpmts.h>
6 #include "lib/rpmplugin.h"
7
8 struct logstat {
9     int logging;
10     unsigned int scriptfail;
11     unsigned int pkgfail;
12 };
13
14 static rpmRC syslog_init(rpmPlugin plugin, rpmts ts)
15 {
16     /* XXX make this configurable? */
17     const char * log_ident = "[RPM]";
18     struct logstat * state = rcalloc(1, sizeof(*state));
19
20     rpmPluginSetData(plugin, state);
21     openlog(log_ident, (LOG_PID), LOG_USER);
22     return RPMRC_OK;
23 }
24
25 static void syslog_cleanup(rpmPlugin plugin)
26 {
27     struct logstat * state = rpmPluginGetData(plugin);
28     free(state);
29     closelog();
30 }
31
32 static rpmRC syslog_tsm_pre(rpmPlugin plugin, rpmts ts)
33 {
34     struct logstat * state = rpmPluginGetData(plugin);
35     
36     /* Reset counters */
37     state->scriptfail = 0;
38     state->pkgfail = 0;
39
40     /* Assume we are logging but... */
41     state->logging = 1;
42
43     /* ...don't log test transactions */
44     if (rpmtsFlags(ts) & (RPMTRANS_FLAG_TEST|RPMTRANS_FLAG_BUILD_PROBS))
45         state->logging = 0;
46
47     /* ...don't log chroot transactions */
48     if (!rstreq(rpmtsRootDir(ts), "/"))
49         state->logging = 0;
50
51     if (state->logging) {
52         syslog(LOG_NOTICE, "Transaction ID %x started", rpmtsGetTid(ts));
53     }
54
55     return RPMRC_OK;
56 }
57
58 static rpmRC syslog_tsm_post(rpmPlugin plugin, rpmts ts, int res)
59 {
60     struct logstat * state = rpmPluginGetData(plugin);
61
62     if (state->logging) {
63         if (state->pkgfail || state->scriptfail) {
64             syslog(LOG_WARNING, "%u elements failed, %u scripts failed",
65                    state->pkgfail, state->scriptfail);
66         }
67         syslog(LOG_NOTICE, "Transaction ID %x finished: %d",
68                 rpmtsGetTid(ts), res);
69     }
70
71     state->logging = 0;
72     return RPMRC_OK;
73 }
74
75 static rpmRC syslog_psm_post(rpmPlugin plugin, rpmte te, int res)
76 {
77     struct logstat * state = rpmPluginGetData(plugin);
78
79     if (state->logging) {
80         int lvl = LOG_NOTICE;
81         const char *op = (rpmteType(te) == TR_ADDED) ? "install" : "erase";
82         const char *outcome = "success";
83         /* XXX: Permit configurable header queryformat? */
84         const char *pkg = rpmteNEVRA(te);
85
86         if (res != RPMRC_OK) {
87             lvl = LOG_WARNING;
88             outcome = "failure";
89             state->pkgfail++;
90         }
91
92         syslog(lvl, "%s %s: %s", op, pkg, outcome);
93     }
94     return RPMRC_OK;
95 }
96
97 static rpmRC syslog_scriptlet_post(rpmPlugin plugin,
98                                         const char *s_name, int type, int res)
99 {
100     struct logstat * state = rpmPluginGetData(plugin);
101
102     if (state->logging && res) {
103         syslog(LOG_WARNING, "scriptlet %s failure: %d\n", s_name, res);
104         state->scriptfail++;
105     }
106     return RPMRC_OK;
107 }
108
109 struct rpmPluginHooks_s syslog_hooks = {
110     .init = syslog_init,
111     .cleanup = syslog_cleanup,
112     .tsm_pre = syslog_tsm_pre,
113     .tsm_post = syslog_tsm_post,
114     .psm_post = syslog_psm_post,
115     .scriptlet_post = syslog_scriptlet_post,
116 };