1099
1104
const char *id = message_data->id;
1100
1105
const char *dso = thread->dso_data->dso_name;
1101
1106
const char *dev = thread->device.uuid;
1102
1108
unsigned events = ((thread->status == DM_THREAD_RUNNING)
1103
1109
&& (thread->events)) ? thread->events : thread->
1104
1110
events | DM_EVENT_REGISTRATION_PENDING;
1106
1112
dm_free(msg->data);
1108
msg->size = dm_asprintf(&(msg->data), fmt, id, dso, dev, events);
1114
if ((r = dm_asprintf(&(msg->data), fmt, id, dso, dev, events)) < 0) {
1119
msg->size = (uint32_t) r;
1241
1247
/* Open fifos used for client communication. */
1242
1248
static int _open_fifos(struct dm_event_fifos *fifos)
1245
1250
struct stat st;
1247
1252
/* Create client fifo. */
1248
1253
(void) dm_prepare_selinux_context(fifos->client_path, S_IFIFO);
1249
1254
if ((mkfifo(fifos->client_path, 0600) == -1) && errno != EEXIST) {
1250
syslog(LOG_ERR, "%s: Failed to create client fifo.\n", __func__);
1255
syslog(LOG_ERR, "%s: Failed to create client fifo %s: %m.\n",
1256
__func__, fifos->client_path);
1252
1257
(void) dm_prepare_selinux_context(NULL, 0);
1257
1261
/* Create server fifo. */
1258
1262
(void) dm_prepare_selinux_context(fifos->server_path, S_IFIFO);
1259
1263
if ((mkfifo(fifos->server_path, 0600) == -1) && errno != EEXIST) {
1260
syslog(LOG_ERR, "%s: Failed to create server fifo.\n", __func__);
1264
syslog(LOG_ERR, "%s: Failed to create server fifo %s: %m.\n",
1265
__func__, fifos->server_path);
1262
1266
(void) dm_prepare_selinux_context(NULL, 0);
1267
1270
(void) dm_prepare_selinux_context(NULL, 0);
1269
1272
/* Warn about wrong permissions if applicable */
1270
1273
if ((!stat(fifos->client_path, &st)) && (st.st_mode & 0777) != 0600)
1271
syslog(LOG_WARNING, "Fixing wrong permissions on %s",
1274
syslog(LOG_WARNING, "Fixing wrong permissions on %s: %m.\n",
1272
1275
fifos->client_path);
1274
1277
if ((!stat(fifos->server_path, &st)) && (st.st_mode & 0777) != 0600)
1275
syslog(LOG_WARNING, "Fixing wrong permissions on %s",
1278
syslog(LOG_WARNING, "Fixing wrong permissions on %s: %m.\n",
1276
1279
fifos->server_path);
1278
1281
/* If they were already there, make sure permissions are ok. */
1279
1282
if (chmod(fifos->client_path, 0600)) {
1280
syslog(LOG_ERR, "Unable to set correct file permissions on %s",
1283
syslog(LOG_ERR, "Unable to set correct file permissions on %s: %m.\n",
1281
1284
fifos->client_path);
1285
1288
if (chmod(fifos->server_path, 0600)) {
1286
syslog(LOG_ERR, "Unable to set correct file permissions on %s",
1289
syslog(LOG_ERR, "Unable to set correct file permissions on %s: %m.\n",
1287
1290
fifos->server_path);
1291
1294
/* Need to open read+write or we will block or fail */
1292
1295
if ((fifos->server = open(fifos->server_path, O_RDWR)) < 0) {
1296
syslog(LOG_ERR, "Failed to open fifo server %s: %m.\n",
1297
fifos->server_path);
1297
1301
/* Need to open read+write for select() to work. */
1298
1302
if ((fifos->client = open(fifos->client_path, O_RDWR)) < 0) {
1300
close(fifos->server);
1303
syslog(LOG_ERR, "Failed to open fifo client %s: %m", fifos->client_path);
1304
if (close(fifos->server))
1305
syslog(LOG_ERR, "Failed to close fifo server %s: %m", fifos->server_path);
1654
1664
return _set_oom_adj(OOM_ADJ_FILE, OOM_SCORE_ADJ_MIN);
1667
static int _handle_preloaded_fifo(int fd, const char *path)
1669
struct stat st_fd, st_path;
1672
if ((flags = fcntl(fd, F_GETFD)) < 0)
1675
if (flags & FD_CLOEXEC)
1678
if (fstat(fd, &st_fd) < 0 || !S_ISFIFO(st_fd.st_mode))
1681
if (stat(path, &st_path) < 0 ||
1682
st_path.st_dev != st_fd.st_dev ||
1683
st_path.st_ino != st_fd.st_ino)
1686
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
1692
static int _systemd_handover(struct dm_event_fifos *fifos)
1696
unsigned long env_pid, env_listen_fds;
1699
memset(fifos, 0, sizeof(*fifos));
1701
/* LISTEN_PID must be equal to our PID! */
1702
if (!(e = getenv(SD_LISTEN_PID_ENV_VAR_NAME)))
1706
env_pid = strtoul(e, &p, 10);
1707
if (errno || !p || *p || env_pid <= 0 ||
1708
getpid() != (pid_t) env_pid)
1711
/* LISTEN_FDS must be 2 and the fds must be FIFOSs! */
1712
if (!(e = getenv(SD_LISTEN_FDS_ENV_VAR_NAME)))
1716
env_listen_fds = strtoul(e, &p, 10);
1717
if (errno || !p || *p || env_listen_fds != 2)
1720
/* Check and handle the FIFOs passed in */
1721
r = (_handle_preloaded_fifo(SD_FD_FIFO_SERVER, DM_EVENT_FIFO_SERVER) &&
1722
_handle_preloaded_fifo(SD_FD_FIFO_CLIENT, DM_EVENT_FIFO_CLIENT));
1725
fifos->server = SD_FD_FIFO_SERVER;
1726
fifos->server_path = DM_EVENT_FIFO_SERVER;
1727
fifos->client = SD_FD_FIFO_CLIENT;
1728
fifos->client_path = DM_EVENT_FIFO_CLIENT;
1732
unsetenv(SD_LISTEN_PID_ENV_VAR_NAME);
1733
unsetenv(SD_LISTEN_FDS_ENV_VAR_NAME);
1658
1738
static void remove_lockfile(void)
1781
_initial_registrations = dm_malloc(sizeof(char*) * (count + 1));
1863
if (!(_initial_registrations = dm_malloc(sizeof(char*) * (count + 1)))) {
1864
fprintf(stderr, "Memory allocation registration failed.\n");
1782
1868
for (i = 0; i < count; ++i) {
1783
_initial_registrations[i] = dm_strdup(message);
1869
if (!(_initial_registrations[i] = dm_strdup(message))) {
1870
fprintf(stderr, "Memory allocation for message failed.\n");
1784
1873
message += strlen(message) + 1;
1786
1875
_initial_registrations[count] = 0;
1793
1882
fini_fifos(&fifos);
1796
static int _handle_preloaded_fifo(int fd, const char *path)
1798
struct stat st_fd, st_path;
1801
if ((flags = fcntl(fd, F_GETFD)) < 0)
1804
if (flags & FD_CLOEXEC)
1807
if (fstat(fd, &st_fd) < 0 || !S_ISFIFO(st_fd.st_mode))
1810
if (stat(path, &st_path) < 0 ||
1811
st_path.st_dev != st_fd.st_dev ||
1812
st_path.st_ino != st_fd.st_ino)
1815
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
1821
static int _systemd_handover(struct dm_event_fifos *fifos)
1825
unsigned long env_pid, env_listen_fds;
1828
memset(fifos, 0, sizeof(*fifos));
1830
/* LISTEN_PID must be equal to our PID! */
1831
if (!(e = getenv(SD_LISTEN_PID_ENV_VAR_NAME)))
1835
env_pid = strtoul(e, &p, 10);
1836
if (errno || !p || *p || env_pid <= 0 ||
1837
getpid() != (pid_t) env_pid)
1840
/* LISTEN_FDS must be 2 and the fds must be FIFOSs! */
1841
if (!(e = getenv(SD_LISTEN_FDS_ENV_VAR_NAME)))
1845
env_listen_fds = strtoul(e, &p, 10);
1846
if (errno || !p || *p || env_listen_fds != 2)
1849
/* Check and handle the FIFOs passed in */
1850
r = (_handle_preloaded_fifo(SD_FD_FIFO_SERVER, DM_EVENT_FIFO_SERVER) &&
1851
_handle_preloaded_fifo(SD_FD_FIFO_CLIENT, DM_EVENT_FIFO_CLIENT));
1854
fifos->server = SD_FD_FIFO_SERVER;
1855
fifos->server_path = DM_EVENT_FIFO_SERVER;
1856
fifos->client = SD_FD_FIFO_CLIENT;
1857
fifos->client_path = DM_EVENT_FIFO_CLIENT;
1861
unsetenv(SD_LISTEN_PID_ENV_VAR_NAME);
1862
unsetenv(SD_LISTEN_FDS_ENV_VAR_NAME);
1866
1885
static void usage(char *prog, FILE *file)
1868
1887
fprintf(file, "Usage:\n"
1869
"%s [-V] [-h] [-d] [-d] [-d] [-f]\n\n"
1870
" -V Show version of dmeventd\n"
1871
" -h Show this help information\n"
1888
"%s [-d [-d [-d]]] [-f] [-h] [-R] [-V] [-?]\n\n"
1872
1889
" -d Log debug messages to syslog (-d, -dd, -ddd)\n"
1873
" -f Don't fork, run in the foreground\n\n", prog);
1890
" -f Don't fork, run in the foreground\n"
1891
" -h -? Show this help information\n"
1892
" -R Restart dmeventd\n"
1893
" -V Show version of dmeventd\n\n", prog);
1876
1896
int main(int argc, char *argv[])