make error() portable

error() is not posix but gnu extension so may not be available on all
kind of systemsi e.g. musl.

Signed-off-by: Randy 'ayaka' Li <ayaka@soulik.info>
Signed-off-by: Randy Li <randy.li@rock-chips.com>
dev
Randy Li 6 years ago committed by Daniel Stone
parent 1bdf363295
commit a1450a8a71
  1. 157
      libweston/weston-launch.c

@ -33,7 +33,6 @@
#include <poll.h>
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <sys/types.h>
@ -235,11 +234,17 @@ setup_pam(struct weston_launch *wl)
static int
setup_launcher_socket(struct weston_launch *wl)
{
if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, wl->sock) < 0)
error(1, errno, "socketpair failed");
if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, wl->sock) < 0) {
fprintf(stderr, "weston: socketpair failed: %s\n",
strerror(errno));
return -1;
}
if (fcntl(wl->sock[0], F_SETFD, FD_CLOEXEC) < 0)
error(1, errno, "fcntl failed");
if (fcntl(wl->sock[0], F_SETFD, FD_CLOEXEC) < 0) {
fprintf(stderr, "weston: fcntl failed: %s\n",
strerror(errno));
return -1;
}
return 0;
}
@ -474,7 +479,8 @@ handle_signal(struct weston_launch *wl)
int pid, status, ret;
if (read(wl->signalfd, &sig, sizeof sig) != sizeof sig) {
error(0, errno, "reading signalfd failed");
fprintf(stderr, "weston: reading signalfd failed: %s\n",
strerror(errno));
return -1;
}
@ -540,49 +546,83 @@ setup_tty(struct weston_launch *wl, const char *tty)
int tty0 = open("/dev/tty0", O_WRONLY | O_CLOEXEC);
char filename[16];
if (tty0 < 0)
error(1, errno, "could not open tty0");
if (tty0 < 0) {
fprintf(stderr, "weston: could not open tty0: %s\n",
strerror(errno));
return -1;
}
if (ioctl(tty0, VT_OPENQRY, &wl->ttynr) < 0 || wl->ttynr == -1)
error(1, errno, "failed to find non-opened console");
{
fprintf(stderr, "weston: failed to find non-opened console: %s\n",
strerror(errno));
return -1;
}
snprintf(filename, sizeof filename, "/dev/tty%d", wl->ttynr);
wl->tty = open(filename, O_RDWR | O_NOCTTY);
close(tty0);
}
if (wl->tty < 0)
error(1, errno, "failed to open tty");
if (wl->tty < 0) {
fprintf(stderr, "weston: failed to open tty: %s\n",
strerror(errno));
return -1;
}
if (fstat(wl->tty, &buf) == -1 ||
major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0)
error(1, 0, "weston-launch must be run from a virtual terminal");
major(buf.st_rdev) != TTY_MAJOR || minor(buf.st_rdev) == 0) {
fprintf(stderr, "weston: weston-launch must be run from a virtual terminal\n");
return -1;
}
if (tty) {
if (fstat(wl->tty, &buf) < 0)
error(1, errno, "stat %s failed", tty);
if (fstat(wl->tty, &buf) < 0) {
fprintf(stderr, "weston: stat %s failed: %s\n", tty,
strerror(errno));
return -1;
}
if (major(buf.st_rdev) != TTY_MAJOR)
error(1, 0, "invalid tty device: %s", tty);
if (major(buf.st_rdev) != TTY_MAJOR) {
fprintf(stderr,
"weston: invalid tty device: %s\n", tty);
return -1;
}
wl->ttynr = minor(buf.st_rdev);
}
if (ioctl(wl->tty, KDGKBMODE, &wl->kb_mode))
error(1, errno, "failed to get current keyboard mode: %m\n");
if (ioctl(wl->tty, KDGKBMODE, &wl->kb_mode)) {
fprintf(stderr,
"weston: failed to get current keyboard mode: %s",
strerror(errno));
return -1;
}
if (ioctl(wl->tty, KDSKBMUTE, 1) &&
ioctl(wl->tty, KDSKBMODE, K_OFF))
error(1, errno, "failed to set K_OFF keyboard mode: %m\n");
ioctl(wl->tty, KDSKBMODE, K_OFF)) {
fprintf(stderr,
"weston: failed to set K_OFF keyboard mode: %s\n",
strerror(errno));
return -1;
}
if (ioctl(wl->tty, KDSETMODE, KD_GRAPHICS))
error(1, errno, "failed to set KD_GRAPHICS mode on tty: %m\n");
if (ioctl(wl->tty, KDSETMODE, KD_GRAPHICS)) {
fprintf(stderr,
"weston: failed to set KD_GRAPHICS mode on tty: %s\n",
strerror(errno));
return -1;
}
mode.mode = VT_PROCESS;
mode.relsig = SIGUSR1;
mode.acqsig = SIGUSR2;
if (ioctl(wl->tty, VT_SETMODE, &mode) < 0)
error(1, errno, "failed to take control of vt handling\n");
if (ioctl(wl->tty, VT_SETMODE, &mode) < 0) {
fprintf(stderr,
"weston: failed to take control of vt handling %s\n",
strerror(errno));
return -1;
}
return 0;
}
@ -595,10 +635,16 @@ setup_session(struct weston_launch *wl, char **child_argv)
int i;
if (wl->tty != STDIN_FILENO) {
if (setsid() < 0)
error(1, errno, "setsid failed");
if (ioctl(wl->tty, TIOCSCTTY, 0) < 0)
error(1, errno, "TIOCSCTTY failed - tty is in use");
if (setsid() < 0) {
fprintf(stderr, "weston: setsid failed %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
if (ioctl(wl->tty, TIOCSCTTY, 0) < 0) {
fprintf(stderr, "TIOCSCTTY failed - tty is in use %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
term = getenv("TERM");
@ -614,7 +660,7 @@ setup_session(struct weston_launch *wl, char **child_argv)
if (env) {
for (i = 0; env[i]; ++i) {
if (putenv(env[i]) != 0)
error(0, 0, "putenv %s failed", env[i]);
fprintf(stderr, "putenv %s failed\n", env[i]);
}
free(env);
}
@ -638,8 +684,11 @@ drop_privileges(struct weston_launch *wl)
#ifdef HAVE_INITGROUPS
initgroups(wl->pw->pw_name, wl->pw->pw_gid) < 0 ||
#endif
setuid(wl->pw->pw_uid) < 0)
error(1, errno, "dropping privileges failed");
setuid(wl->pw->pw_uid) < 0) {
fprintf(stderr, "weston: dropping privileges failed %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
}
static void
@ -678,7 +727,8 @@ launch_compositor(struct weston_launch *wl, int argc, char *argv[])
execv(child_argv[0], child_argv);
error(1, errno, "exec failed");
fprintf(stderr, "weston: exec failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
static void
@ -713,8 +763,10 @@ main(int argc, char *argv[])
switch (c) {
case 'u':
wl.new_user = optarg;
if (getuid() != 0)
error(1, 0, "Permission denied. -u allowed for root only");
if (getuid() != 0) {
fprintf(stderr, "weston: Permission denied. -u allowed for root only\n");
exit(EXIT_FAILURE);
}
break;
case 't':
tty = optarg;
@ -730,27 +782,38 @@ main(int argc, char *argv[])
}
}
if ((argc - optind) > (MAX_ARGV_SIZE - 6))
error(1, E2BIG, "Too many arguments to pass to weston");
if ((argc - optind) > (MAX_ARGV_SIZE - 6)) {
fprintf(stderr,
"weston: Too many arguments to pass to weston: %s\n",
strerror(E2BIG));
exit(EXIT_FAILURE);
}
if (tty && !wl.new_user)
error(1, 0, "-t/--tty option requires -u/--user option as well");
if (tty && !wl.new_user) {
fprintf(stderr, "weston: -t/--tty option requires -u/--user option as well\n");
exit(EXIT_FAILURE);
}
if (wl.new_user)
wl.pw = getpwnam(wl.new_user);
else
wl.pw = getpwuid(getuid());
if (wl.pw == NULL)
error(1, errno, "failed to get username");
if (wl.pw == NULL) {
fprintf(stderr, "weston: failed to get username: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
if (!weston_launch_allowed(&wl))
error(1, 0, "Permission denied. You should either:\n"
if (!weston_launch_allowed(&wl)) {
fprintf(stderr, "Permission denied. You should either:\n"
#ifdef HAVE_SYSTEMD_LOGIN
" - run from an active and local (systemd) session.\n"
#else
" - enable systemd session support for weston-launch.\n"
#endif
" - or add yourself to the 'weston-launch' group.");
" - or add yourself to the 'weston-launch' group.\n");
exit(EXIT_FAILURE);
}
if (setup_tty(&wl, tty) < 0)
exit(EXIT_FAILURE);
@ -765,8 +828,10 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
wl.child = fork();
if (wl.child == -1)
error(EXIT_FAILURE, errno, "fork failed");
if (wl.child == -1) {
fprintf(stderr, "weston: fork failed %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (wl.child == 0)
launch_compositor(&wl, argc - optind, argv + optind);

Loading…
Cancel
Save