2017-04-19 23:48:54 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Jehanne.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* Jehanne 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 Affero General Public License
|
|
|
|
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <u.h>
|
|
|
|
#include <lib9.h>
|
|
|
|
#include <posix.h>
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
extern int *__libposix_errors_codes;
|
2017-05-01 23:57:05 +02:00
|
|
|
extern WaitList **__libposix_wait_list;
|
2017-05-24 00:40:28 +02:00
|
|
|
extern ChildList **__libposix_child_list;
|
2017-04-19 23:48:54 +02:00
|
|
|
static int __initialized;
|
2017-09-11 01:01:11 +02:00
|
|
|
static PosixProcessDisposer __libposix_process_dispose;
|
2017-05-29 02:04:48 +02:00
|
|
|
|
2017-09-11 01:01:11 +02:00
|
|
|
int *__libposix_sigchld_father_pid;
|
|
|
|
int *__libposix_sigchld_child_pid;
|
|
|
|
int *__libposix_devsignal;
|
|
|
|
int *__libposix_pid;
|
2017-05-25 01:04:53 +02:00
|
|
|
|
2017-04-19 23:48:54 +02:00
|
|
|
static void
|
|
|
|
libposix_check_configuration(void)
|
|
|
|
{
|
|
|
|
__libposix_errors_check_conf();
|
|
|
|
__libposix_files_check_conf();
|
|
|
|
__libposix_processes_check_conf();
|
2017-09-11 01:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
__libposix_sighelper_open(void)
|
|
|
|
{
|
|
|
|
int mypid;
|
|
|
|
if(*__libposix_devsignal >= 0)
|
|
|
|
close(*__libposix_devsignal);
|
|
|
|
mypid = *__libposix_pid;
|
|
|
|
*__libposix_devsignal = create("/dev/posix/signals", ORDWR|OCEXEC, mypid);
|
|
|
|
if(*__libposix_devsignal < 0)
|
|
|
|
sysfatal("cannot create /dev/posix/signals: %r");
|
2017-04-19 23:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libposix_init(int argc, char *argv[], PosixInit init)
|
|
|
|
{
|
|
|
|
extern int main(int, char**);
|
2017-05-05 19:01:18 +02:00
|
|
|
extern int *__handling_external_signal;
|
2017-05-29 02:04:48 +02:00
|
|
|
extern int *__restart_syscall;
|
2017-09-11 01:01:11 +02:00
|
|
|
extern PosixSignalMask *__libposix_signal_mask;
|
2017-05-05 19:01:18 +02:00
|
|
|
|
2017-05-01 23:57:05 +02:00
|
|
|
WaitList *wait_list;
|
2017-05-24 00:40:28 +02:00
|
|
|
ChildList *child_list;
|
2017-09-11 01:01:11 +02:00
|
|
|
PosixSignalMask signal_mask;
|
|
|
|
int mypid;
|
2017-04-19 23:48:54 +02:00
|
|
|
int status;
|
2017-05-01 23:57:05 +02:00
|
|
|
int error_codes[ERRNO_LAST-ERRNO_FIRST];
|
2017-05-05 19:01:18 +02:00
|
|
|
int handling_signal;
|
2017-09-11 01:01:11 +02:00
|
|
|
int sigchld_father_pid;
|
|
|
|
int sigchld_child_pid;
|
2017-05-29 02:04:48 +02:00
|
|
|
int restart_syscall;
|
2017-09-11 01:01:11 +02:00
|
|
|
int devsignal;
|
2017-04-19 23:48:54 +02:00
|
|
|
|
|
|
|
assert(__initialized == 0);
|
|
|
|
|
2017-09-11 01:01:11 +02:00
|
|
|
mypid = getpid();
|
|
|
|
__libposix_pid = &mypid;
|
|
|
|
|
2017-04-19 23:48:54 +02:00
|
|
|
/* initialize PosixErrors map */
|
|
|
|
memset(error_codes, 0, sizeof(error_codes));
|
2017-09-11 01:01:11 +02:00
|
|
|
__libposix_errors_codes = error_codes;
|
2017-05-02 00:57:18 +02:00
|
|
|
|
2017-05-01 23:57:05 +02:00
|
|
|
/* initialize wait_list; see also POSIX_fork and POSIX_exit */
|
|
|
|
wait_list = nil;
|
|
|
|
__libposix_wait_list = &wait_list;
|
2017-04-19 23:48:54 +02:00
|
|
|
|
2017-05-24 00:04:30 +02:00
|
|
|
/* initialize child_list; used when SIGCHLD is enabled */
|
|
|
|
child_list = nil;
|
2017-05-24 00:40:28 +02:00
|
|
|
__libposix_child_list = &child_list;
|
2017-05-24 00:04:30 +02:00
|
|
|
|
2017-05-05 19:01:18 +02:00
|
|
|
/* initialize signal handling */
|
|
|
|
handling_signal = 0;
|
2017-09-11 01:01:11 +02:00
|
|
|
sigchld_father_pid = 0;
|
|
|
|
sigchld_child_pid = 0;
|
|
|
|
signal_mask = 0;
|
|
|
|
devsignal = -1;
|
|
|
|
__libposix_devsignal = &devsignal;
|
2017-05-29 02:04:48 +02:00
|
|
|
__restart_syscall = &restart_syscall;
|
2017-05-05 19:01:18 +02:00
|
|
|
__handling_external_signal = &handling_signal;
|
2017-09-11 01:01:11 +02:00
|
|
|
__libposix_sigchld_father_pid = &sigchld_father_pid;
|
|
|
|
__libposix_sigchld_child_pid = &sigchld_child_pid;
|
|
|
|
__libposix_signal_mask = &signal_mask;
|
|
|
|
__libposix_reset_pending_signals();
|
2017-04-19 23:48:54 +02:00
|
|
|
if(!atnotify(__libposix_note_handler, 1))
|
|
|
|
sysfatal("libposix: atnotify");
|
|
|
|
|
2017-09-11 01:01:11 +02:00
|
|
|
__libposix_sighelper_open();
|
|
|
|
signal_mask = (PosixSignalMask)__libposix_sighelper_cmd(PHGetProcMask, 0);
|
|
|
|
__libposix_init_signal_handlers();
|
|
|
|
|
|
|
|
init(argc, argv);
|
2017-04-19 23:48:54 +02:00
|
|
|
|
|
|
|
libposix_check_configuration();
|
|
|
|
|
|
|
|
__initialized = 1;
|
|
|
|
status = main(argc, argv);
|
2017-09-11 01:01:11 +02:00
|
|
|
|
|
|
|
if(__libposix_process_dispose != nil)
|
|
|
|
__libposix_process_dispose(status);
|
|
|
|
|
2017-04-19 23:48:54 +02:00
|
|
|
POSIX_exit(status);
|
|
|
|
}
|
|
|
|
|
2017-09-11 01:01:11 +02:00
|
|
|
int
|
|
|
|
libposix_on_process_disposition(PosixProcessDisposer dispose)
|
|
|
|
{
|
|
|
|
if(__libposix_process_dispose != nil)
|
|
|
|
return 0;
|
|
|
|
__libposix_process_dispose = dispose;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:48:54 +02:00
|
|
|
int
|
|
|
|
__libposix_initialized(void)
|
|
|
|
{
|
|
|
|
return __initialized;
|
|
|
|
}
|
2017-09-11 01:01:11 +02:00
|
|
|
|
|
|
|
long
|
|
|
|
__libposix_sighelper_cmd(PosixHelperCommand command, int posix_process_pid)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
PosixHelperRequest request;
|
|
|
|
long raw;
|
|
|
|
} offset;
|
|
|
|
|
|
|
|
offset.request.command = command;
|
|
|
|
offset.request.target = posix_process_pid;
|
|
|
|
|
|
|
|
return pwrite(*__libposix_devsignal, "", 0, offset.raw);
|
|
|
|
}
|