Pass object to child process

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
n0p0n3
Posts: 10
Joined: Fri Jun 25, 2021 11:11 pm

Pass object to child process

Post by n0p0n3 »

I need to pass an object from init to child (and on to each child after that) basically a global variable thats essential to how my os works. I can't pass it using the vfs because its the root vfs node to bootstrap access to the vfs in the first place. I can however assign the page frame its stored on to the child on creation, but dont know how it would be accessed. Would you just use it like normal once assigned, or is there some other way to use it? I can't find the right wording to search for to find the answer. Once the child has the root vfs node, everything after is golden, just having this one last bootstrapping problem. I'm using a single address space (no virtual addressing) and all programs compiled with pic
Last edited by n0p0n3 on Sun Jul 18, 2021 2:31 am, edited 1 time in total.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Pass object to child process

Post by iansjack »

Can't you just store the variable at a fixed memory location?
n0p0n3
Posts: 10
Joined: Fri Jun 25, 2021 11:11 pm

Re: Pass object to child process

Post by n0p0n3 »

I'd seen that as a solution but didnt know if it was a good way of going about it. Once the process gets it, all other things become possible (process scheduling, memory management, device drivers). I could also pass it by modifying the main function to accept it but I'm trying to stay posix and I dont know how that would break things. I'm also using netbsd rumpkernel stuff so I have to dive into the vfs code to see what's there, there very well may be something that I don't know about there that helps.
nullplan
Member
Member
Posts: 1790
Joined: Wed Aug 30, 2017 8:24 am

Re: Pass object to child process

Post by nullplan »

Pass the object on the stack? Or pass the object anywhere in user space, and add an auxiliary vector that points to it? That's how Linux injects the VDSO.
Carpe diem!
n0p0n3
Posts: 10
Joined: Fri Jun 25, 2021 11:11 pm

Re: Pass object to child process

Post by n0p0n3 »

How would I do either of those? I've basically got a unikernel running, and once done will have multiple unikernels running, with scheduling, and access controls. Untill then, the only way I can think of other than using a set memory address is to have each Unikernel read from the hdd or a ramdisk or something and which requires drivers, thus negating the whole idea of having them embedded in the vfs v_ops in the first place.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Pass object to child process

Post by nexos »

What I would do is send the v_ops struct as a parameter to init's main(), and then have init pass it on to children as parameters as well.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
n0p0n3
Posts: 10
Joined: Fri Jun 25, 2021 11:11 pm

Re: Pass object to child process

Post by n0p0n3 »

Would it work if I pass the root vfs node address through the args and cast to a pointer? That way I could have the address randomized and not stored as an environ variable for all to see. Also it would ensure all my processes are using the same root vfs node. Its a single flat address space so no virtual addresses and I can have the page assigned to each child on creation for access control. When I search for it, people say it can't work because each process has the same virtual address but different physical addresses, but I can't find much about flat addressing and casting pointers from strings in combo. I was reading a stackoverflow question where one of the answers is to run the text string containing memory address through sscanf and convert to hex, then typecast to void pointer. Would this work, cause that would probably be perfect.

https://stackoverflow.com/questions/415 ... inter-in-c
MollenOS
Member
Member
Posts: 202
Joined: Wed Oct 26, 2011 12:00 pm

Re: Pass object to child process

Post by MollenOS »

When you initialize the stack for the process's main thread, I would just push all the data you need onto the top of the stack, and then end with a pointer to itself so that becomes the first argument to your process's init function.
n0p0n3
Posts: 10
Joined: Fri Jun 25, 2021 11:11 pm

Re: Pass object to child process

Post by n0p0n3 »

I went looking for this and found alot I didnt know about happening under the hood that isn't written about very well, but this would probably be the best way. I found a code snippet that explains a way to fetch the data, but I am not sure how good it is.

<stdio.h>
#include <elf.h>

main(int argc, char* argv[], char* envp[])
{
Elf32_auxv_t *auxv;
while (*envp++ != NULL); /* from stack diagram above: *envp = NULL marks end of envp */

for (auxv = (Elf32_auxv_t *)envp; auxv->a_type != AT_NULL; auxv++)
/* auxv->a_type = AT_NULL marks the end of auxv */
{
if (auxv->a_type == AT_SYSINFO)
printf("AT_SYSINFO is: 0x%x\n", auxv->a_un.a_val);
}
}

Seems like its casting the envp string to aux_t and iterating through. Wouldnt this cause problems since strings are chars and the aux_t struct are different sizes? Wouldn't you risk iterating into the middle of an element?
Or is it iterating through to the end of the string, then once more to reach the auxv? I did see something to that effect somewhere.
Otherwise in likeing this method of passing the needed info.
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Pass object to child process

Post by davmac314 »

n0p0n3 wrote:Seems like its casting the envp string to aux_t and iterating through. Wouldnt this cause problems since strings are chars and the aux_t struct are different sizes? Wouldn't you risk iterating into the middle of an element?
envp isn't a string, it's a pointer to the first element in an array of pointers to strings.

The code you posted iterates through the array until it reaches the last one, indicated by a null. Then it goes one further, which is where the auxillary vector is; it casts one pointer to another pointer type. It's not casting a string to a pointer, it's casting a pointer to a pointer.
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: Pass object to child process

Post by Octocontrabass »

Keep in mind main() is not the entry point. You have your startup code that calls main(), and you can put whatever you want in there to make it more convenient to pass the object to the rest of your program.
n0p0n3
Posts: 10
Joined: Fri Jun 25, 2021 11:11 pm

Re: Pass object to child process

Post by n0p0n3 »

Yeah I had a total brain fart about the string thing, I definitely think this is the best way to go about it
Post Reply