]> git.ipfire.org Git - thirdparty/util-linux.git/blame - lib/plymouth-ctrl.c
kill: add missing ifdefs
[thirdparty/util-linux.git] / lib / plymouth-ctrl.c
CommitLineData
fe3f7e17 1/*
9e930041 2 * plymouth-ctrl.c Simply communications with plymouthd
fe3f7e17 3 * to avoid forked sub processes and/or
9e930041
SR
4 * missed plymouth send commands tool
5 * due a plymouthd replacement.
fe3f7e17
WF
6 *
7 * Copyright (c) 2016 SUSE Linux GmbH, All rights reserved.
8 * Copyright (c) 2016 Werner Fink <werner@suse.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program (see the file COPYING); if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 * MA 02110-1301, USA.
24 *
25 * Author: Werner Fink <werner@suse.de>
26 */
27
28#include <errno.h>
29#include <limits.h>
30#include <poll.h>
31#include <signal.h>
32#include <stdarg.h>
33#include <stdlib.h>
34#include <sys/mman.h>
35#include <sys/socket.h>
36#include <sys/types.h>
37#include <sys/un.h>
38#include <unistd.h>
39
40#include "all-io.h"
41#include "c.h"
42#include "nls.h"
43#include "plymouth-ctrl.h"
44
45static int can_read(int fd, const long timeout)
46{
47 struct pollfd fds = {
48 .fd = fd,
49 .events = POLLIN|POLLPRI,
50 .revents = 0,
51 };
52 int ret;
53
54 do {
55 ret = poll(&fds, 1, timeout);
56 } while ((ret < 0) && (errno == EINTR));
57
58 return (ret == 1) && (fds.revents & (POLLIN|POLLPRI));
59}
60
61static int open_un_socket_and_connect(void)
62{
db981e3e
RM
63 /* The abstract UNIX socket of plymouth */
64 struct sockaddr_un su = {
fe3f7e17
WF
65 .sun_family = AF_UNIX,
66 .sun_path = PLYMOUTH_SOCKET_PATH,
67 };
68 const int one = 1;
69 int fd, ret;
70
71 fd = socket(PF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
72 if (fd < 0) {
223939d9 73 warnx(_("cannot open UNIX socket"));
fe3f7e17
WF
74 goto err;
75 }
76
db981e3e 77 ret = setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
fe3f7e17 78 if (ret < 0) {
223939d9 79 warnx(_("cannot set option for UNIX socket"));
fe3f7e17
WF
80 close(fd);
81 fd = -1;
82 goto err;
83 }
84
db981e3e
RM
85 /* Note, the abstract PLYMOUTH_SOCKET_PATH has a leading NULL byte */
86 ret = connect(fd, (struct sockaddr *) &su,
87 offsetof(struct sockaddr_un, sun_path) + 1 + strlen(su.sun_path+1));
fe3f7e17
WF
88 if (ret < 0) {
89 if (errno != ECONNREFUSED)
223939d9 90 warnx(_("cannot connect on UNIX socket"));
fe3f7e17
WF
91 close(fd);
92 fd = -1;
93 goto err;
94 }
95err:
96 return fd;
97}
98
99int plymouth_command(int cmd, ...)
100{
101 uint8_t answer[2], command[2];
102 struct sigaction sp, op;
103 int fdsock = -1, ret = 0;
104
105 sigemptyset (&sp.sa_mask);
106 sp.sa_handler = SIG_IGN;
107 sp.sa_flags = SA_RESTART;
108 sigaction(SIGPIPE, &sp, &op);
109
db981e3e 110 /* The plymouthd does read at least two bytes. */
fe3f7e17
WF
111 command[1] = '\0';
112 switch (cmd) {
113 case MAGIC_PING:
114 fdsock = open_un_socket_and_connect();
115 if (fdsock >= 0) {
116 command[0] = cmd;
117 write_all(fdsock, command, sizeof(command));
118 }
119 break;
120 case MAGIC_QUIT:
121 fdsock = open_un_socket_and_connect();
122 if (fdsock >= 0) {
123 command[0] = cmd;
124 write_all(fdsock, command, sizeof(command));
125 }
126 break;
127 default:
128 warnx(_("the plymouth request %c is not implemented"), cmd);
129 case '?':
130 goto err;
131 }
132
133 answer[0] = '\0';
134 if (fdsock >= 0) {
135 if (can_read(fdsock, 1000))
136 read_all(fdsock, (char *) &answer[0], sizeof(answer));
137 close(fdsock);
138 }
139 sigaction(SIGPIPE, &op, NULL);
140 ret = (answer[0] == ANSWER_ACK) ? 1 : 0;
141err:
142 return ret;
143}
144