glibc might compile for x86 now
[akaros.git] / kern / include / ros / procinfo.h
1 /* See COPYRIGHT for copyright information. */
2
3 #ifndef ROS_PROCINFO_H
4 #define ROS_PROCINFO_H
5
6 #include <ros/memlayout.h>
7 #include <ros/common.h>
8
9 #define PROCINFO_MAX_ARGP 32
10 #define PROCINFO_ARGBUF_SIZE 3072
11
12 typedef struct procinfo {
13         pid_t pid;
14         pid_t ppid;
15         size_t max_harts;
16         uint64_t tsc_freq;
17
18         char* argp[PROCINFO_MAX_ARGP];
19         char argbuf[PROCINFO_ARGBUF_SIZE];
20 } procinfo_t;
21 #define PROCINFO_NUM_PAGES  ((sizeof(procinfo_t)-1)/PGSIZE + 1) 
22
23 static int
24 procinfo_pack_args(procinfo_t* p, char* const* argv, char* const* envp)
25 {
26         int nargv = 0, nenvp = 0;
27         if(argv) while(argv[nargv]) nargv++;
28         if(envp) while(envp[nenvp]) nenvp++;
29
30         if(nargv+nenvp+2 > PROCINFO_MAX_ARGP)
31                 return -1;
32
33         int pos = 0;
34         for(int i = 0; i < nargv; i++)
35         {
36                 int len = strlen(argv[i])+1;
37                 if(pos+len > PROCINFO_ARGBUF_SIZE)
38                         return -1;
39                 p->argp[i] = ((procinfo_t*)UINFO)->argbuf+pos;
40                 memcpy(p->argbuf+pos,argv[i],len);
41                 pos += len;
42         }
43         p->argp[nargv] = 0;
44
45         for(int i = 0; i < nenvp; i++)
46         {
47                 int len = strlen(envp[i])+1;
48                 if(pos+len > PROCINFO_ARGBUF_SIZE)
49                         return -1;
50                 p->argp[nargv+1+i] = ((procinfo_t*)UINFO)->argbuf+pos;
51                 memcpy(p->argbuf+pos,envp[i],len);
52                 pos += len;
53         }
54         p->argp[nargv+nenvp+1] = 0;
55         
56         return 0;
57 }
58
59 // this is how user programs access the procinfo page
60 #ifndef ROS_KERNEL
61 # define __procinfo (*(procinfo_t*)UINFO)
62 #endif
63
64 #endif // !ROS_PROCDATA_H