]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/bsdload.c
bsd-user: put back a break; that had gone missing...
[mirror_qemu.git] / bsd-user / bsdload.c
CommitLineData
84778508
BS
1/* Code for loading BSD executables. Mostly linux kernel code. */
2
2231197c 3#include "qemu/osdep.h"
84778508
BS
4
5#include "qemu.h"
6
e99a22cc 7#define TARGET_NGROUPS 32
84778508
BS
8
9/* ??? This should really be somewhere else. */
10abi_long memcpy_to_target(abi_ulong dest, const void *src,
11 unsigned long len)
12{
13 void *host_ptr;
14
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;
21}
22
ca0fd2e3 23static int count(char **vec)
84778508
BS
24{
25 int i;
26
ca0fd2e3 27 for (i = 0; *vec; i++) {
84778508
BS
28 vec++;
29 }
30
fa054637 31 return i;
84778508
BS
32}
33
34static int prepare_binprm(struct linux_binprm *bprm)
35{
36 struct stat st;
37 int mode;
4a65a86a 38 int retval;
84778508 39
ca0fd2e3 40 if (fstat(bprm->fd, &st) < 0) {
fa054637 41 return -errno;
84778508
BS
42 }
43
44 mode = st.st_mode;
ca0fd2e3 45 if (!S_ISREG(mode)) { /* Must be regular file */
fa054637 46 return -EACCES;
84778508 47 }
ca0fd2e3 48 if (!(mode & 0111)) { /* Must have at least one execute bit set */
fa054637 49 return -EACCES;
84778508
BS
50 }
51
52 bprm->e_uid = geteuid();
53 bprm->e_gid = getegid();
84778508
BS
54
55 /* Set-uid? */
ca0fd2e3 56 if (mode & S_ISUID) {
84778508 57 bprm->e_uid = st.st_uid;
84778508
BS
58 }
59
60 /* Set-gid? */
61 /*
62 * If setgid is set but no group execute bit then this
63 * is a candidate for mandatory locking, not a setgid
64 * executable.
65 */
66 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
67 bprm->e_gid = st.st_gid;
84778508
BS
68 }
69
70 memset(bprm->buf, 0, sizeof(bprm->buf));
71 retval = lseek(bprm->fd, 0L, SEEK_SET);
ca0fd2e3 72 if (retval >= 0) {
84778508
BS
73 retval = read(bprm->fd, bprm->buf, 128);
74 }
ca0fd2e3 75 if (retval < 0) {
84778508
BS
76 perror("prepare_binprm");
77 exit(-1);
84778508
BS
78 }
79 else {
fa054637 80 return retval;
84778508
BS
81 }
82}
83
84/* Construct the envp and argv tables on the target stack. */
85abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
86 abi_ulong stringp, int push_ptr)
87{
88 int n = sizeof(abi_ulong);
89 abi_ulong envp;
90 abi_ulong argv;
91
92 sp -= (envc + 1) * n;
93 envp = sp;
94 sp -= (argc + 1) * n;
95 argv = sp;
96 if (push_ptr) {
97 /* FIXME - handle put_user() failures */
98 sp -= n;
99 put_user_ual(envp, sp);
100 sp -= n;
101 put_user_ual(argv, sp);
102 }
103 sp -= n;
104 /* FIXME - handle put_user() failures */
105 put_user_ual(argc, sp);
106
107 while (argc-- > 0) {
108 /* FIXME - handle put_user() failures */
109 put_user_ual(stringp, argv);
110 argv += n;
111 stringp += target_strlen(stringp) + 1;
112 }
113 /* FIXME - handle put_user() failures */
114 put_user_ual(0, argv);
115 while (envc-- > 0) {
116 /* FIXME - handle put_user() failures */
117 put_user_ual(stringp, envp);
118 envp += n;
119 stringp += target_strlen(stringp) + 1;
120 }
121 /* FIXME - handle put_user() failures */
122 put_user_ual(0, envp);
123
124 return sp;
125}
126
ca0fd2e3
WL
127int loader_exec(const char *filename, char **argv, char **envp,
128 struct target_pt_regs *regs, struct image_info *infop)
84778508
BS
129{
130 struct linux_binprm bprm;
131 int retval;
132 int i;
133
ca0fd2e3
WL
134 bprm.p = TARGET_PAGE_SIZE * MAX_ARG_PAGES - sizeof(unsigned int);
135 for (i = 0 ; i < MAX_ARG_PAGES ; i++) /* clear page-table */
7cba04f6 136 bprm.page[i] = NULL;
84778508
BS
137 retval = open(filename, O_RDONLY);
138 if (retval < 0)
139 return retval;
140 bprm.fd = retval;
141 bprm.filename = (char *)filename;
142 bprm.argc = count(argv);
143 bprm.argv = argv;
144 bprm.envc = count(envp);
145 bprm.envp = envp;
146
147 retval = prepare_binprm(&bprm);
148
ca0fd2e3 149 if (retval >= 0) {
84778508
BS
150 if (bprm.buf[0] == 0x7f
151 && bprm.buf[1] == 'E'
152 && bprm.buf[2] == 'L'
153 && bprm.buf[3] == 'F') {
ca0fd2e3 154 retval = load_elf_binary(&bprm, regs, infop);
84778508 155 } else {
9bb93180 156 fprintf(stderr, "Unknown binary format\n");
84778508
BS
157 return -1;
158 }
159 }
160
ca0fd2e3 161 if (retval >= 0) {
84778508
BS
162 /* success. Initialize important registers */
163 do_init_thread(regs, infop);
164 return retval;
165 }
166
167 /* Something went wrong, return the inode and free the argument pages*/
ca0fd2e3 168 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
c580dee4 169 g_free(bprm.page[i]);
84778508 170 }
fa054637 171 return retval;
84778508 172}