2020-08-23 08:22:14 -05:00
|
|
|
#include <rpc.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <serdes.h>
|
2020-07-22 19:35:23 -05:00
|
|
|
#include <dbg.h>
|
2020-08-23 08:22:14 -05:00
|
|
|
#include <sys/types.h>
|
2020-07-22 19:35:23 -05:00
|
|
|
#include <stdlib.h>
|
2020-08-23 08:22:14 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2020-08-23 08:46:38 -05:00
|
|
|
#include <string.h>
|
2019-08-31 16:46:52 -05:00
|
|
|
|
|
|
|
int num_devs;
|
|
|
|
int max_devs;
|
|
|
|
char** dev_names;
|
2020-08-23 08:22:14 -05:00
|
|
|
pid_t* dev_pids;
|
2019-08-31 16:46:52 -05:00
|
|
|
|
2020-08-23 08:22:14 -05:00
|
|
|
void devfs_mount(void* args) {
|
|
|
|
serdes_state state;
|
|
|
|
serialize_int(0,&state);
|
|
|
|
serialize_ptr(NULL,&state);
|
|
|
|
rpc_return(state.buf,state.sizeorpos);
|
|
|
|
free(state.buf);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
2019-09-01 14:15:01 -05:00
|
|
|
|
2020-08-23 08:22:14 -05:00
|
|
|
void open(void* args) {
|
|
|
|
serdes_state state;
|
|
|
|
start_deserialize(args,&state);
|
|
|
|
char* path=deserialize_str(&state);
|
2020-08-23 14:05:38 -05:00
|
|
|
deserialize_ptr(&state);
|
2020-08-23 08:22:14 -05:00
|
|
|
rpc_deallocate_buf(args,state.sizeorpos);
|
2019-09-05 14:12:04 -05:00
|
|
|
int i;
|
|
|
|
char found=0;
|
|
|
|
for (i=0;i<num_devs;i++) {
|
2020-08-23 08:22:14 -05:00
|
|
|
if (strcmp(dev_names[i],path)==0) {
|
2019-09-05 14:12:04 -05:00
|
|
|
found=1;
|
2019-08-31 16:46:52 -05:00
|
|
|
break;
|
|
|
|
}
|
2019-09-05 14:12:04 -05:00
|
|
|
}
|
|
|
|
if (!found) {
|
2020-08-23 08:22:14 -05:00
|
|
|
state.buf=NULL;
|
|
|
|
state.sizeorpos=0;
|
|
|
|
serialize_int(1,&state);
|
|
|
|
serialize_ptr(NULL,&state);
|
|
|
|
serialize_int(0,&state);
|
|
|
|
rpc_return(state.buf,state.sizeorpos);
|
|
|
|
pthread_exit(NULL);
|
2019-09-05 14:12:04 -05:00
|
|
|
}
|
2020-08-23 08:22:14 -05:00
|
|
|
state.buf=NULL;
|
|
|
|
state.sizeorpos=0;
|
|
|
|
serialize_int(0,&state);
|
|
|
|
serialize_ptr(NULL,&state);
|
|
|
|
serialize_int(dev_pids[i],&state);
|
|
|
|
rpc_return(state.buf,state.sizeorpos);
|
|
|
|
free(state.buf);
|
|
|
|
pthread_exit(NULL);
|
2019-09-05 14:12:04 -05:00
|
|
|
}
|
|
|
|
|
2020-08-23 08:22:14 -05:00
|
|
|
void register_dev(void* args) {
|
|
|
|
serdes_state state;
|
|
|
|
start_deserialize(args,&state);
|
|
|
|
char* name=deserialize_str(&state);
|
|
|
|
pid_t pid=deserialize_int(&state);
|
|
|
|
rpc_deallocate_buf(args,state.sizeorpos);
|
|
|
|
if (num_devs==max_devs) {
|
|
|
|
max_devs+=32;
|
|
|
|
dev_names=realloc(dev_names,sizeof(char*)*max_devs);
|
|
|
|
dev_pids=realloc(dev_pids,sizeof(pid_t)*max_devs);
|
2019-09-05 14:12:04 -05:00
|
|
|
}
|
2020-08-23 08:22:14 -05:00
|
|
|
dev_names[num_devs]=name;
|
|
|
|
dev_pids[num_devs]=pid;
|
|
|
|
num_devs++;
|
|
|
|
rpc_return(NULL,0);
|
|
|
|
pthread_exit(NULL);
|
2019-09-05 14:12:04 -05:00
|
|
|
}
|
|
|
|
|
2019-07-13 10:18:41 -05:00
|
|
|
int main() {
|
2020-08-23 08:22:14 -05:00
|
|
|
num_devs=0;
|
|
|
|
max_devs=32;
|
2019-08-31 16:46:52 -05:00
|
|
|
dev_names=malloc(sizeof(char*)*32);
|
2020-08-23 08:22:14 -05:00
|
|
|
dev_pids=malloc(sizeof(pid_t)*32);
|
|
|
|
rpc_register_func("mount",&devfs_mount);
|
|
|
|
rpc_register_func("open",&open);
|
|
|
|
rpc_register_func("register_dev",®ister_dev);
|
|
|
|
register_fs("devfs",getpid());
|
|
|
|
serial_print("Initialized devfs\n");
|
2020-08-23 08:46:38 -05:00
|
|
|
rpc_mark_as_init();
|
2019-07-13 10:18:41 -05:00
|
|
|
}
|
2020-08-23 08:22:14 -05:00
|
|
|
|