Skip to content

Commit

Permalink
sysdeps/ironclad: Miscellaneous sysdeps updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mintsuki committed Feb 9, 2024
1 parent db3f9c5 commit 24f2eb3
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 133 deletions.
307 changes: 203 additions & 104 deletions sysdeps/ironclad/generic/generic.cpp

Large diffs are not rendered by default.

28 changes: 7 additions & 21 deletions sysdeps/ironclad/generic/mount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,17 @@
#include <sys/syscall.h>
#include <string.h>

int mount(const char *source, const char *target,
const char *fstype, unsigned long flags, const void *data) {
int ret, errno;
int mount(const char *source, const char *target, int type, int flags) {
int ret;
size_t source_len = strlen(source);
size_t target_len = strlen(target);
int val;
if (!strcmp(fstype, "ext")) {
val = 1;
} else if (!strcmp(fstype, "fat32")) {
val = 2;
} else {
return EINVAL;
}

SYSCALL6(SYSCALL_MOUNT, source, source_len, target, target_len, val, flags);
return errno;
}

int umount(const char *target) {
return umount2(target, 0);
SYSCALL6(SYSCALL_MOUNT, source, source_len, target, target_len, type, flags);
return ret;
}

int umount2(const char *target, int flags) {
int ret, errno;
int umount(const char *target, int flags) {
int ret;
size_t target_len = strlen(target);
SYSCALL3(SYSCALL_UMOUNT, target, target_len, flags);
return errno;
return ret;
}
27 changes: 25 additions & 2 deletions sysdeps/ironclad/generic/pty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,35 @@
#include <unistd.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/ioctl.h>

int openpty(int *mfd, int *sfd, char *name, const struct termios *ios, const struct winsize *win) {
int errno, ret;
int ret;
int fds[2];
SYSCALL3(SYSCALL_OPENPTY, fds, ios, win);
SYSCALL1(SYSCALL_OPENPTY, fds);
if (errno) {
return -1;
}
*mfd = fds[0];
*sfd = fds[1];

if (name != NULL) {
ret = ttyname_r(*mfd, name, (size_t)-1);
if (ret) {
return -1;
}
}
if (ios != NULL) {
ret = tcsetattr(*mfd, TCSANOW, ios);
if (ret) {
return -1;
}
}
if (win != NULL) {
ret = ioctl(*mfd, TIOCGWINSZ, win);
if (ret) {
return -1;
}
}
return ret;
}
76 changes: 76 additions & 0 deletions sysdeps/ironclad/generic/utmpx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <bits/ensure.h>
#include <stddef.h>
#include <errno.h>
#include <utmpx.h>
#include <stdio.h>
#include <time.h>
#include <paths.h>
#include <unistd.h>
#include <fcntl.h>

int utmpx_file = -1;

void updwtmpx(const char *, const struct utmpx *) {
// Empty as musl does
}

void endutxent(void) {
if (utmpx_file >= 0) {
close(utmpx_file);
}
}

void setutxent(void) {
if (utmpx_file < 0) {
utmpx_file = open(UTMPX_FILE, O_RDWR | O_CREAT, 0755);
} else {
lseek(utmpx_file, 0, SEEK_SET);
}
}

struct utmpx returned;

struct utmpx *getutxent(void) {
if (utmpx_file < 0) {
setutxent();
if (utmpx_file < 0) {
return NULL;
}
}

if (read(utmpx_file, &returned, sizeof(struct utmpx)) != sizeof(struct utmpx)) {
return NULL;
}

return &returned;
}

struct utmpx *pututxline(const struct utmpx *added) {
if (utmpx_file < 0) {
setutxent();
if (utmpx_file < 0) {
return NULL;
}
}

lseek(utmpx_file, 0, SEEK_END);
if (write(utmpx_file, added, sizeof(struct utmpx)) != sizeof(struct utmpx)) {
return NULL;
}

return (struct utmpx *)added;
}

int utmpxname(const char *path) {
if (utmpx_file > 0) {
close(utmpx_file);
}

utmpx_file = open(path, O_RDWR | O_CREAT, 0755);
if (utmpx_file > 0) {
lseek(utmpx_file, 0, SEEK_END);
return 1;
} else {
return 0;
}
}
13 changes: 9 additions & 4 deletions sysdeps/ironclad/include/sys/mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
extern "C" {
#endif

#define MNT_EXT 1
#define MNT_FAT 2

#define MS_RDONLY 0b001
#define MS_REMOUNT 0b010
#define MS_RELATIME 0b100

#define MNT_FORCE 1

int mount(const char *source, const char *target,
const char *fstype, unsigned long flags, const void *data);
int umount(const char *target);
int umount2(const char *target, int flags);
int mount(const char *source, const char *target, int type, int flags);
int umount(const char *target, int flags);

#ifdef __cplusplus
}
Expand Down
17 changes: 17 additions & 0 deletions sysdeps/ironclad/include/sys/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,22 @@
#define SYSCALL_ACTUALLY_KILL 79
#define SYSCALL_SIGNALPOST 80
#define SYSCALL_SEND_SIGNAL 81
#define SYSCALL_GETPRIO 82
#define SYSCALL_SETPRIO 83
#define SYSCALL_GETGID 84
#define SYSCALL_GETEGID 85
#define SYSCALL_SETGIDS 86
#define SYSCALL_GETGROUPS 87
#define SYSCALL_SETGROUPS 88
#define SYSCALL_TTYNAME 89
#define SYSCALL_FADVISE 90
#define SYSCALL_SHMAT 91
#define SYSCALL_SHMCTL 92
#define SYSCALL_SHMDT 93
#define SYSCALL_SHMGET 94
#define SYSCALL_GETSOCKOPT 95
#define SYSCALL_SETSOCKOPT 96
#define SYSCALL_GETTIDID 97
#define SYSCALL_SETTIDID 98

#endif // _SYS_SYSCALL_H
63 changes: 63 additions & 0 deletions sysdeps/ironclad/include/utmpx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

#ifndef _UTMPX_H
#define _UTMPX_H

#ifdef __cplusplus
extern "C" {
#endif

#include <abi-bits/pid_t.h>
#include <bits/posix/timeval.h>

#define UTMPX_FILE "/var/run/utmp"
#define WTMPX_FILE "/var/run/wtmp"

// Struct definition taken from musl
struct utmpx {
short ut_type;
short __ut_pad1;
pid_t ut_pid;
char ut_line[32];
char ut_id[4];
char ut_user[32];
char ut_host[256];
struct {
short __e_termination;
short __e_exit;
} ut_exit;
int ut_session, __ut_pad2;
struct timeval ut_tv;
unsigned ut_addr_v6[4];
char __unused[20];
};

#ifndef __MLIBC_ABI_ONLY

void updwtmpx(const char *, const struct utmpx *);
int utmpxname(const char *);
struct utmpx *pututxline(const struct utmpx *);
struct utmpx *getutxent(void);
void setutxent(void);
void endutxent(void);

#endif /* !__MLIBC_ABI_ONLY */

#define EMPTY 0
#define RUN_LVL 1
#define BOOT_TIME 2
#define NEW_TIME 3
#define OLD_TIME 4
#define INIT_PROCESS 5
#define LOGIN_PROCESS 6
#define USER_PROCESS 7
#define DEAD_PROCESS 8

#define __UT_HOSTSIZE 256
#define __UT_NAMESIZE 32
#define __UT_LINESIZE 32

#ifdef __cplusplus
}
#endif

#endif // _UTMPX_H
6 changes: 4 additions & 2 deletions sysdeps/ironclad/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ libc_sources += files(
'generic/thread.cpp',
'generic/mac.cpp',
'generic/sched2.cpp',
'generic/thread.S'
'generic/thread.S',
'generic/utmpx.cpp'
)

if not no_headers
Expand Down Expand Up @@ -88,7 +89,8 @@ if not no_headers

install_headers(
'include/mntent.h',
'include/pty.h'
'include/pty.h',
'include/utmpx.h'
)
endif

Expand Down

0 comments on commit 24f2eb3

Please sign in to comment.