Skip to content

Commit 4585798

Browse files
committed
Forward argv along with spawnl/spawnv syscall
1 parent 6de7825 commit 4585798

File tree

6 files changed

+52
-9
lines changed

6 files changed

+52
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ binaries: $(bt_stage1) $(bt_stage2) $(kernel_core) $(rm_static)
102102
SECTOR_COUNT_BT_STAGE1 = 1
103103
SECTOR_COUNT_SHARED_LIBRARY = 1
104104
SECTOR_COUNT_BT_STAGE2 = 11
105-
SECTOR_COUNT_KERNEL = 44
105+
SECTOR_COUNT_KERNEL = 45
106106

107107
SECTOR_START_BT_STAGE1 = 0
108108
SECTOR_START_SHARED_LIBRARY = $(shell expr $(SECTOR_START_BT_STAGE1) + $(SECTOR_COUNT_BT_STAGE1) )

src/kernel/core.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <string.h>
88
#include <process.h>
9+
#include <stddef.h>
910
#include <sys/syscall.h>
1011
#include <conio.h>
1112

@@ -48,7 +49,7 @@ void kernel_core_entry() {
4849

4950
clrscr();
5051

51-
int init_pid = spawn(INIT_APPNAME);
52+
int init_pid = spawnl(INIT_APPNAME, INIT_APPNAME, NULL);
5253
print_log("init process created: %d", init_pid);
5354

5455
// interrupt_pit_enable();

src/kernel/process/process.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,22 @@ int syscall_1_process_exec_lba_sc(int lba_start, int sector_count) {
4646
return process_exec(lba_start, sector_count);
4747
}
4848

49-
int syscall_1_process_spawn_fname(int user_ds, char *_us_filename) {
49+
int syscall_1_process_spawn_fname(int user_ds, char *_us_filename, char *_us_argv[]) {
5050
char filename[FS_FFS_FILENAME_LIMIT];
51+
char argv_with_uspointer[PROCESS_MAX_ARGC];
52+
char *argv_with_kspointer[PROCESS_MAX_ARGC];
53+
char argv[PROCESS_MAX_ARGC][PROCESS_MAX_ARG_LEN]; // data store
5154
syscall_strncpy_user_to_kernel(user_ds, _us_filename, filename, sizeof(filename));
52-
55+
syscall_strncpy_user_to_kernel(user_ds, _us_argv, argv_with_uspointer, sizeof(argv_with_uspointer));
56+
// if src string is NULL, then dst should be null.
57+
for (int i = 0; i < PROCESS_MAX_ARGC; i++) {
58+
if(argv_with_uspointer[i]==NULL) {
59+
argv_with_kspointer[i]=NULL;
60+
break;
61+
}
62+
syscall_strncpy_user_to_kernel(user_ds, argv[i], argv_with_uspointer[i], sizeof(argv[i]));
63+
argv_with_kspointer[i] = argv[i];
64+
}
5365

5466
union FFSFileEntry entry;
5567
int file_id = file_handler_find(filename, &entry);
@@ -58,6 +70,8 @@ int syscall_1_process_spawn_fname(int user_ds, char *_us_filename) {
5870

5971
int lba_start = resolve_abs_lba(FFS_UNIQUE_PARITION_ID, entry.content.start_block_id);
6072
int sector_count = (entry.content.filesize + FS_BLOCK_SIZE -1)/FS_BLOCK_SIZE;
73+
74+
// TODO: process created should have argv pushed on top of stack.
6175
return syscall_1_process_spawn_lba_sc(lba_start, sector_count);
6276
}
6377

@@ -71,7 +85,7 @@ int syscall_1_process(int operation, int a0, int a1, int a2, int a3, int user_ds
7185
case SYSCALL_PROCESS_SUB_EXEC_LBA_SC:
7286
return syscall_1_process_exec_lba_sc(a0, a1);
7387
case SYSCALL_PROCESS_SUB_SPAWN_FNAME:
74-
return syscall_1_process_spawn_fname(user_ds, (char*)a0);
88+
return syscall_1_process_spawn_fname(user_ds, (char*)a0, (char**)a1);
7589
}
7690
return -1;
7791
}

src/usr/include/process.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
#define SYSCALL_PROCESS_SUB_EXEC_LBA_SC 2
66
#define SYSCALL_PROCESS_SUB_SPAWN_FNAME 3
77

8+
#define PROCESS_MAX_ARGC 32
9+
#define PROCESS_MAX_ARG_LEN 64
10+
811
// if non-negative value is returned
912
// then it's the pid of the new process
1013
// otherwise it's some error code
11-
int spawn(char *path);
14+
int spawnl(char *path, char *arg0, ...);
15+
int spawnv(char *path, char *argv[]);
1216

1317
void yield();

src/usr/lib/process.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
#include <process.h>
2+
#include <stddef.h>
3+
#include <stdarg.h>
24
#include <sys/syscall.h>
35

4-
int spawn(char *file_path) {
5-
int pid = SYSCALL_A2(SYSCALL_PROCESS, SYSCALL_PROCESS_SUB_SPAWN_FNAME, file_path);
6+
int spawnl(char *file_path, char *arg0, ...) {
7+
va_list args;
8+
va_start(args, arg0);
9+
char *argv[PROCESS_MAX_ARGC];
10+
11+
// prepare argv
12+
{
13+
int i = 0;
14+
while(i<PROCESS_MAX_ARGC-1) {
15+
char *arg = va_arg(args, char *);
16+
if(arg==NULL) break;
17+
argv[i] = arg;
18+
i++;
19+
}
20+
argv[i] = NULL;
21+
}
22+
23+
int rv = spawnv(file_path, argv);
24+
va_end(args);
25+
return rv;
26+
}
27+
28+
int spawnv(char *file_path, char *argv[]) {
29+
int pid = SYSCALL_A3(SYSCALL_PROCESS, SYSCALL_PROCESS_SUB_SPAWN_FNAME, file_path, argv);
630
if(pid>=0) yield();
731
return pid;
832
}

src/usr/local/src/sh.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int cmd_run(char *fname) {
2828
printf("usage: run <filename>\n");
2929
return -2;
3030
}
31-
return spawn(fname);
31+
return spawnl(fname, fname, NULL);
3232
}
3333

3434
int cmd_echo(char *text) {

0 commit comments

Comments
 (0)