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
|
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* This file is part of LVM2.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v.2.1.
*/
#include "lib/misc/lib.h"
#include "lib/commands/toolcontext.h"
#include "lib/notify/lvmnotify.h"
#define LVM_DBUS_DESTINATION "com.redhat.lvmdbus1"
#define LVM_DBUS_PATH "/com/redhat/lvmdbus1/Manager"
#define LVM_DBUS_INTERFACE "com.redhat.lvmdbus1.Manager"
#define LVM_DBUS_LOCK_FILE "/var/lock/lvm/lvmdbusd"
#define LVM_DBUS_LOCK_FILE_ENV_KEY "LVM_DBUSD_LOCKFILE"
#define SD_BUS_SYSTEMD_NO_SUCH_UNIT_ERROR "org.freedesktop.systemd1.NoSuchUnit"
#define SD_BUS_DBUS_SERVICE_UNKNOWN_ERROR "org.freedesktop.DBus.Error.ServiceUnknown"
#ifdef NOTIFYDBUS_SUPPORT
#include <systemd/sd-bus.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int lvmnotify_is_supported(void)
{
return 1;
}
static int lvmdbusd_running(void)
{
int fd = 0;
int rc = 0;
int errno_cpy = 0;
int running = 0;
const char *lockfile = NULL;
/*
* lvm dbusd uses a lock file with a lock on it, thus to determine if the daemon is running
* requires that you attempt to lock the file as well. Thus the existence of the file does
* not mean it's running, but the absence of the file does indicate it's not running.
*
* See lvmdbusd for more details.
*/
lockfile = getenv(LVM_DBUS_LOCK_FILE_ENV_KEY);
if (!lockfile) {
lockfile = LVM_DBUS_LOCK_FILE;
}
errno = 0;
fd = open(lockfile, O_RDWR);
if (-1 == fd) {
errno_cpy = errno;
if (errno_cpy == ENOENT) {
return 0;
} else {
/* Safest option is to return running when we encounter unexpected errors */
log_debug_dbus("Unexpected errno: %d on lockfile open, returning running", errno_cpy);
return 1;
}
}
/* Need to ensure we close lock FD now */
errno = 0;
rc = lockf(fd, F_TLOCK|F_TEST, 0);
if (-1 != rc) {
/* Not locked, thus not running */
running = 0;
} else {
errno_cpy = errno;
if (errno_cpy == EACCES || errno_cpy == EAGAIN) {
/* Locked, so daemon is running */
running = 1;
} else {
log_debug_dbus("Unexpected errno: %d on lockf, returning running", errno_cpy);
running = 1 ;
}
}
close(fd);
return running;
}
void lvmnotify_send(struct cmd_context *cmd)
{
static const char _dbus_notification_failed_msg[] = "D-Bus notification failed";
sd_bus *bus = NULL;
sd_bus_message *m = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
const char *cmd_name;
int ret;
int result = 0;
if (!cmd->vg_notify && !cmd->lv_notify && !cmd->pv_notify)
return;
cmd->vg_notify = 0;
cmd->lv_notify = 0;
cmd->pv_notify = 0;
/* If lvmdbusd isn't running, don't notify as you will start it as it will auto activate */
if (!lvmdbusd_running()) {
log_debug_dbus("dbus damon not running, not notifying");
return;
}
cmd_name = get_cmd_name();
ret = sd_bus_open_system(&bus);
if (ret < 0) {
log_debug_dbus("Failed to connect to dbus: %d", ret);
return;
}
log_debug_dbus("Nofify dbus at %s.", LVM_DBUS_DESTINATION);
ret = sd_bus_call_method(bus,
LVM_DBUS_DESTINATION,
LVM_DBUS_PATH,
LVM_DBUS_INTERFACE,
"ExternalEvent",
&error,
&m,
"s",
cmd_name);
if (ret < 0) {
if (sd_bus_error_has_name(&error, SD_BUS_SYSTEMD_NO_SUCH_UNIT_ERROR) ||
sd_bus_error_has_name(&error, SD_BUS_DBUS_SERVICE_UNKNOWN_ERROR))
log_debug_dbus("%s: %s", _dbus_notification_failed_msg, error.message);
else
log_warn("WARNING: %s: %s", _dbus_notification_failed_msg, error.message);
goto out;
}
ret = sd_bus_message_read(m, "i", &result);
if (ret < 0)
log_debug_dbus("Failed to parse dbus response message: %d", ret);
if (result)
log_debug_dbus("Bad return value from dbus service: %d", result);
out:
sd_bus_error_free(&error);
sd_bus_message_unref(m);
sd_bus_flush_close_unref(bus);
}
void set_vg_notify(struct cmd_context *cmd)
{
cmd->vg_notify = 1;
}
void set_lv_notify(struct cmd_context *cmd)
{
cmd->lv_notify = 1;
}
void set_pv_notify(struct cmd_context *cmd)
{
cmd->pv_notify = 1;
}
#else
int lvmnotify_is_supported(void)
{
return 0;
}
void lvmnotify_send(struct cmd_context *cmd)
{
}
void set_vg_notify(struct cmd_context *cmd)
{
}
void set_lv_notify(struct cmd_context *cmd)
{
}
void set_pv_notify(struct cmd_context *cmd)
{
}
#endif
|