summaryrefslogtreecommitdiffstats
path: root/askconsole.c
blob: eba4959b93ef85e1dfc884b7293e4902ad3cab7c (plain)
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
/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation
 *
 * 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.
 */

#include "unitd.h"

#include <sys/types.h>
#include <sys/ioctl.h>

#include <fcntl.h>
#include <unistd.h>


static void askconsole(struct uloop_process *proc) {
	char *const ask[] = {
		"/sbin/askfirst",
		"/bin/ash",
		"--login",
		NULL,
	};

	pid_t p;

	proc->pid = fork();
	if (!proc->pid) {
		p = setsid();

		fcntl(STDERR_FILENO, F_SETFL, fcntl(STDERR_FILENO, F_GETFL) & ~O_NONBLOCK);

		ioctl(STDIN_FILENO, TIOCSCTTY, 1);
		tcsetpgrp(STDIN_FILENO, p);

		execvp(ask[0], ask);
		ERROR("Failed to execute %s\n", ask[0]);
		exit(-1);
	}

	if (proc->pid > 0) {
		DEBUG(4, "Launched askconsole, pid=%d\n",
					(int) proc->pid);
		uloop_process_add(proc);
	}
}

static void child_exit(struct uloop_process *proc, int ret)
{
	DEBUG(4, "pid:%d\n", proc->pid);
	askconsole(proc);
}

void unitd_askconsole(void) {
	struct uloop_process *proc = malloc(sizeof(*proc));
	proc->cb = child_exit;
	askconsole(proc);
}