Add environ handling + PATH to sh

This commit is contained in:
Mark 2020-06-15 12:04:18 +03:00
parent b3747676d2
commit 0feedadfd3
6 changed files with 79 additions and 8 deletions

View File

@ -119,7 +119,7 @@ static int loginuid(uid_t uid, gid_t gid, const char *sh) {
ioctl(STDIN_FILENO, TIOCSPGRP, &pid);
const char *argp[] = { sh, NULL };
_exit(execve(sh, (char *const *) argp, NULL));
_exit(execve(sh, (char *const *) argp, environ));
} else {
int st;
waitpid(sh_pid, &st, 0);

19
init.c
View File

@ -18,8 +18,21 @@ static int start_login(void) {
const char *const argp[] = {
login, NULL
};
// No fork needed, I guess
return execve(login, (char *const *) argp, NULL);
int pid = fork();
int res;
switch (pid) {
case 0:
return execve(login, (char *const *) argp, environ);
case -1:
fprintf(stderr, "Init program failed\n");
return -1;
default:
waitpid(pid, &res, 0);
return 0;
}
}
int main(int argc, char **argv) {
@ -30,7 +43,7 @@ int main(int argc, char **argv) {
mount(NULL, "/dev", "devfs", 0, NULL);
mount(NULL, "/sys", "sysfs", 0, NULL);
mount("/dev/sda1", "/mnt", NULL, 0, NULL);
mount("/dev/sda", "/mnt", NULL, 0, NULL);
uint32_t inaddr = 0x0A000001;
netctl("eth0", NETCTL_SET_INADDR, &inaddr);

View File

@ -1,4 +1,5 @@
#include <ygg/module.h>
#include <drivers/pci/pci.h>
#include <sys/debug.h>
#include <stdint.h>
@ -6,11 +7,17 @@ MODULE_DESC("test-mod", 0x00010000);
extern uint64_t system_time;
static void pci_driver(struct pci_device *dev) {
kinfo("Loaded the driver!\n");
}
void MODULE_EXIT(void) {
kinfo("Exiting the module!\n");
}
int MODULE_ENTRY(void) {
kinfo("This should work, I guess: %s, %d\n", "a string", 123);
pci_add_device_driver(PCI_ID(0x8086, 0x2930), pci_driver, "test-mod");
return 0;
}

View File

@ -22,6 +22,14 @@ static struct sh_builtin __builtins[];
////
DEF_BUILTIN(env) {
for (size_t i = 0; environ[i]; ++i) {
printf("%s\n", environ[i]);
}
return 0;
}
DEF_BUILTIN(exit) {
if (cmd->argc > 1) {
exit(atoi(cmd->args[1]));
@ -206,6 +214,17 @@ DEF_BUILTIN(echo) {
return 0;
}
DEF_BUILTIN(set) {
if (cmd->argc != 3) {
fprintf(stderr, "Usage: set <key> <value>\n");
return -1;
}
setenv(cmd->args[1], cmd->args[2], 1);
return 0;
}
// TODO: support usernames (getpwnam_r)
DEF_BUILTIN(setid) {
if (cmd->argc == 2) {
@ -250,8 +269,10 @@ static struct sh_builtin __builtins[] = {
DECL_BUILTIN(chown),
DECL_BUILTIN(clear),
DECL_BUILTIN(echo),
DECL_BUILTIN(env),
DECL_BUILTIN(exec),
DECL_BUILTIN(exit),
DECL_BUILTIN(set),
DECL_BUILTIN(setid),
DECL_BUILTIN(stat),
DECL_BUILTIN(sync),

View File

@ -68,7 +68,7 @@ static int cmd_spawn(const char *path, const struct cmd_exec *cmd, int *cmd_res)
pid = getpid();
ioctl(STDIN_FILENO, TIOCSPGRP, &pid);
setpgid(0, 0);
exit(execve(path, (char *const *) cmd->args, NULL));
exit(execve(path, (char *const *) cmd->args, environ));
} else {
if (waitpid(pid, cmd_res, 0) != 0) {
perror("waitpid()");
@ -96,10 +96,36 @@ static int cmd_exec_binary(const struct cmd_exec *cmd, int *cmd_res) {
return cmd_spawn(path_path, cmd, cmd_res);
}
snprintf(path_path, sizeof(path_path), "%s/%s", PATH, cmd->args[0]);
if ((res = access(path_path, X_OK)) == 0) {
return cmd_spawn(path_path, cmd, cmd_res);
const char *pathvar = getenv("PATH");
if (!pathvar || !*pathvar) {
return -1;
}
const char *p = pathvar;
const char *e;
while (1) {
e = strchr(p, ':');
size_t len;
if (!e) {
len = strlen(p);
} else {
len = e - p;
}
strncpy(path_path, p, len);
path_path[len] = '/';
strcpy(path_path + len + 1, cmd->args[0]);
if ((res = access(path_path, X_OK)) == 0) {
return cmd_spawn(path_path, cmd, cmd_res);
}
if (!e) {
break;
}
p = e + 1;
}
return -1;
}

View File

@ -3,6 +3,7 @@
#include <sys/fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pwd.h>
@ -109,6 +110,9 @@ int main(int argc, char **argv) {
return -1;
}
// TODO: source /etc/profile?
setenv("PATH", "/sbin:/bin", 0);
if (!isatty(fd)) {
while (1) {
if (gets_safe(fd, linebuf, sizeof(linebuf)) < 0) {