qed: Use bdrv_co_* for coroutine_fns
[qemu/kevin.git] / bsd-user / bsdload.c
blob94eec363b21f5dc061f344490419e5ec69b6f9ec
1 /* Code for loading BSD executables. Mostly linux kernel code. */
3 #include "qemu/osdep.h"
5 #include "qemu.h"
7 #define TARGET_NGROUPS 32
9 /* ??? This should really be somewhere else. */
10 abi_long memcpy_to_target(abi_ulong dest, const void *src,
11 unsigned long len)
13 void *host_ptr;
15 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
16 if (!host_ptr)
17 return -TARGET_EFAULT;
18 memcpy(host_ptr, src, len);
19 unlock_user(host_ptr, dest, 1);
20 return 0;
23 static int in_group_p(gid_t g)
25 /* return TRUE if we're in the specified group, FALSE otherwise */
26 int ngroup;
27 int i;
28 gid_t grouplist[TARGET_NGROUPS];
30 ngroup = getgroups(TARGET_NGROUPS, grouplist);
31 for(i = 0; i < ngroup; i++) {
32 if(grouplist[i] == g) {
33 return 1;
36 return 0;
39 static int count(char ** vec)
41 int i;
43 for(i = 0; *vec; i++) {
44 vec++;
47 return(i);
50 static int prepare_binprm(struct linux_binprm *bprm)
52 struct stat st;
53 int mode;
54 int retval, id_change;
56 if(fstat(bprm->fd, &st) < 0) {
57 return(-errno);
60 mode = st.st_mode;
61 if(!S_ISREG(mode)) { /* Must be regular file */
62 return(-EACCES);
64 if(!(mode & 0111)) { /* Must have at least one execute bit set */
65 return(-EACCES);
68 bprm->e_uid = geteuid();
69 bprm->e_gid = getegid();
70 id_change = 0;
72 /* Set-uid? */
73 if(mode & S_ISUID) {
74 bprm->e_uid = st.st_uid;
75 if(bprm->e_uid != geteuid()) {
76 id_change = 1;
80 /* Set-gid? */
82 * If setgid is set but no group execute bit then this
83 * is a candidate for mandatory locking, not a setgid
84 * executable.
86 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
87 bprm->e_gid = st.st_gid;
88 if (!in_group_p(bprm->e_gid)) {
89 id_change = 1;
93 memset(bprm->buf, 0, sizeof(bprm->buf));
94 retval = lseek(bprm->fd, 0L, SEEK_SET);
95 if(retval >= 0) {
96 retval = read(bprm->fd, bprm->buf, 128);
98 if(retval < 0) {
99 perror("prepare_binprm");
100 exit(-1);
101 /* return(-errno); */
103 else {
104 return(retval);
108 /* Construct the envp and argv tables on the target stack. */
109 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
110 abi_ulong stringp, int push_ptr)
112 int n = sizeof(abi_ulong);
113 abi_ulong envp;
114 abi_ulong argv;
116 sp -= (envc + 1) * n;
117 envp = sp;
118 sp -= (argc + 1) * n;
119 argv = sp;
120 if (push_ptr) {
121 /* FIXME - handle put_user() failures */
122 sp -= n;
123 put_user_ual(envp, sp);
124 sp -= n;
125 put_user_ual(argv, sp);
127 sp -= n;
128 /* FIXME - handle put_user() failures */
129 put_user_ual(argc, sp);
131 while (argc-- > 0) {
132 /* FIXME - handle put_user() failures */
133 put_user_ual(stringp, argv);
134 argv += n;
135 stringp += target_strlen(stringp) + 1;
137 /* FIXME - handle put_user() failures */
138 put_user_ual(0, argv);
139 while (envc-- > 0) {
140 /* FIXME - handle put_user() failures */
141 put_user_ual(stringp, envp);
142 envp += n;
143 stringp += target_strlen(stringp) + 1;
145 /* FIXME - handle put_user() failures */
146 put_user_ual(0, envp);
148 return sp;
151 int loader_exec(const char * filename, char ** argv, char ** envp,
152 struct target_pt_regs * regs, struct image_info *infop)
154 struct linux_binprm bprm;
155 int retval;
156 int i;
158 bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
159 for (i=0 ; i<MAX_ARG_PAGES ; i++) /* clear page-table */
160 bprm.page[i] = NULL;
161 retval = open(filename, O_RDONLY);
162 if (retval < 0)
163 return retval;
164 bprm.fd = retval;
165 bprm.filename = (char *)filename;
166 bprm.argc = count(argv);
167 bprm.argv = argv;
168 bprm.envc = count(envp);
169 bprm.envp = envp;
171 retval = prepare_binprm(&bprm);
173 if(retval>=0) {
174 if (bprm.buf[0] == 0x7f
175 && bprm.buf[1] == 'E'
176 && bprm.buf[2] == 'L'
177 && bprm.buf[3] == 'F') {
178 retval = load_elf_binary(&bprm,regs,infop);
179 } else {
180 fprintf(stderr, "Unknown binary format\n");
181 return -1;
185 if(retval>=0) {
186 /* success. Initialize important registers */
187 do_init_thread(regs, infop);
188 return retval;
191 /* Something went wrong, return the inode and free the argument pages*/
192 for (i=0 ; i<MAX_ARG_PAGES ; i++) {
193 g_free(bprm.page[i]);
195 return(retval);