util-linux/lib/plymouth-ctrl.c

149 lines
3.5 KiB
C
Raw Normal View History

/*
* plymouth-ctrl.c Simply communications with plymouthd
* to avoid forked sub processes and/or
* missed plymouth send commands tool
* due a plymouthd replacement.
*
* Copyright (c) 2016 SUSE Linux GmbH, All rights reserved.
* Copyright (c) 2016 Werner Fink <werner@suse.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program (see the file COPYING); if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* Author: Werner Fink <werner@suse.de>
*/
#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "all-io.h"
#include "c.h"
#include "nls.h"
#include "plymouth-ctrl.h"
static int can_read(int fd, const long timeout)
{
struct pollfd fds = {
.fd = fd,
.events = POLLIN|POLLPRI,
.revents = 0,
};
int ret;
do {
ret = poll(&fds, 1, timeout);
} while ((ret < 0) && (errno == EINTR));
return (ret == 1) && (fds.revents & (POLLIN|POLLPRI));
}
static int open_un_socket_and_connect(void)
{
struct sockaddr_un su = { /* The abstract UNIX socket of plymouth */
.sun_family = AF_UNIX,
.sun_path = PLYMOUTH_SOCKET_PATH,
};
const int one = 1;
int fd, ret;
fd = socket(PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
if (fd < 0) {
warnx(_("cannot open UNIX socket"));
goto err;
}
ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, (socklen_t)sizeof(one));
if (ret < 0) {
warnx(_("cannot set option for UNIX socket"));
close(fd);
fd = -1;
goto err;
}
/*
* Please note that the PLYMOUTH_SOCKET_PATH has a
* leading NULL byte to mark it as an abstract socket
*/
misc: fix some warnings sys-utils/prlimit.c: In function 'do_prlimit': sys-utils/prlimit.c:367:16: warning: format '%ju' expects argument of type 'uintmax_t', but argument 2 has type 'rlim_t {aka long long unsigned int}' [-Wformat=] printf("<%ju", new->rlim_cur); lib/plymouth-ctrl.c: In function 'open_un_socket_and_connect': lib/plymouth-ctrl.c:88:20: warning: passing argument 2 of 'connect' from incompatible pointer type [-Wincompatible-pointer-types] ret = connect(fd, &su, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(su.sun_path+1)); ^ In file included from lib/plymouth-ctrl.c:35:0: /usr/include/sys/socket.h:314:5: note: expected 'const struct sockaddr *' but argument is of type 'struct sockaddr_un *' int connect (int, const struct sockaddr *, socklen_t); login-utils/last.c: In function 'list': login-utils/last.c:506:54: warning: pointer targets in passing argument 4 of 'dns_lookup' differ in signedness [-Wpointer-sign] r = dns_lookup(domain, sizeof(domain), ctl->useip, p->ut_addr_v6); ^ login-utils/last.c:291:12: note: expected 'int32_t * {aka int *}' but argument is of type 'unsigned int *' static int dns_lookup(char *result, int size, int useip, int32_t *a) ^~~~~~~~~~ In file included from sys-utils/hwclock-cmos.c:92:0: sys-utils/hwclock.h:67:32: warning: 'struct timeval' declared inside parameter list will not be visible outside of this definition or declaration extern double time_diff(struct timeval subtrahend, struct timeval subtractor); misc-utils/test_uuidd.c: In function 'create_nthreads': misc-utils/test_uuidd.c:187:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] proc->pid, (int) th->tid, th->index)); Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-05-31 16:58:11 -05:00
ret = connect(fd, (const struct sockaddr *) &su,
offsetof(struct sockaddr_un, sun_path) + 1 + strlen(su.sun_path+1));
if (ret < 0) {
if (errno != ECONNREFUSED)
warnx(_("cannot connect on UNIX socket"));
close(fd);
fd = -1;
goto err;
}
err:
return fd;
}
int plymouth_command(int cmd, ...)
{
uint8_t answer[2], command[2];
struct sigaction sp, op;
int fdsock = -1, ret = 0;
sigemptyset (&sp.sa_mask);
sp.sa_handler = SIG_IGN;
sp.sa_flags = SA_RESTART;
sigaction(SIGPIPE, &sp, &op);
/*
* The plymouthd does read at least two bytes.
*/
command[1] = '\0';
switch (cmd) {
case MAGIC_PING:
fdsock = open_un_socket_and_connect();
if (fdsock >= 0) {
command[0] = cmd;
write_all(fdsock, command, sizeof(command));
}
break;
case MAGIC_QUIT:
fdsock = open_un_socket_and_connect();
if (fdsock >= 0) {
command[0] = cmd;
write_all(fdsock, command, sizeof(command));
}
break;
default:
warnx(_("the plymouth request %c is not implemented"), cmd);
case '?':
goto err;
}
answer[0] = '\0';
if (fdsock >= 0) {
if (can_read(fdsock, 1000))
read_all(fdsock, (char *) &answer[0], sizeof(answer));
close(fdsock);
}
sigaction(SIGPIPE, &op, NULL);
ret = (answer[0] == ANSWER_ACK) ? 1 : 0;
err:
return ret;
}