1 /* Code for loading BSD executables. Mostly linux kernel code. */
3 #include "qemu/osdep.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
,
15 host_ptr
= lock_user(VERIFY_WRITE
, dest
, len
, 0);
17 return -TARGET_EFAULT
;
18 memcpy(host_ptr
, src
, len
);
19 unlock_user(host_ptr
, dest
, 1);
23 static int in_group_p(gid_t g
)
25 /* return TRUE if we're in the specified group, FALSE otherwise */
28 gid_t grouplist
[TARGET_NGROUPS
];
30 ngroup
= getgroups(TARGET_NGROUPS
, grouplist
);
31 for(i
= 0; i
< ngroup
; i
++) {
32 if(grouplist
[i
] == g
) {
39 static int count(char ** vec
)
43 for(i
= 0; *vec
; i
++) {
50 static int prepare_binprm(struct linux_binprm
*bprm
)
54 int retval
, id_change
;
56 if(fstat(bprm
->fd
, &st
) < 0) {
61 if(!S_ISREG(mode
)) { /* Must be regular file */
64 if(!(mode
& 0111)) { /* Must have at least one execute bit set */
68 bprm
->e_uid
= geteuid();
69 bprm
->e_gid
= getegid();
74 bprm
->e_uid
= st
.st_uid
;
75 if(bprm
->e_uid
!= geteuid()) {
82 * If setgid is set but no group execute bit then this
83 * is a candidate for mandatory locking, not a setgid
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
)) {
93 memset(bprm
->buf
, 0, sizeof(bprm
->buf
));
94 retval
= lseek(bprm
->fd
, 0L, SEEK_SET
);
96 retval
= read(bprm
->fd
, bprm
->buf
, 128);
99 perror("prepare_binprm");
101 /* return(-errno); */
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
);
116 sp
-= (envc
+ 1) * n
;
118 sp
-= (argc
+ 1) * n
;
121 /* FIXME - handle put_user() failures */
123 put_user_ual(envp
, sp
);
125 put_user_ual(argv
, sp
);
128 /* FIXME - handle put_user() failures */
129 put_user_ual(argc
, sp
);
132 /* FIXME - handle put_user() failures */
133 put_user_ual(stringp
, argv
);
135 stringp
+= target_strlen(stringp
) + 1;
137 /* FIXME - handle put_user() failures */
138 put_user_ual(0, argv
);
140 /* FIXME - handle put_user() failures */
141 put_user_ual(stringp
, envp
);
143 stringp
+= target_strlen(stringp
) + 1;
145 /* FIXME - handle put_user() failures */
146 put_user_ual(0, envp
);
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
;
158 bprm
.p
= TARGET_PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(unsigned int);
159 for (i
=0 ; i
<MAX_ARG_PAGES
; i
++) /* clear page-table */
161 retval
= open(filename
, O_RDONLY
);
165 bprm
.filename
= (char *)filename
;
166 bprm
.argc
= count(argv
);
168 bprm
.envc
= count(envp
);
171 retval
= prepare_binprm(&bprm
);
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
);
180 fprintf(stderr
, "Unknown binary format\n");
186 /* success. Initialize important registers */
187 do_init_thread(regs
, infop
);
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
]);