]> git.proxmox.com Git - mirror_qemu.git/blob - bsd-user/bsdload.c
bsd-user: add license to bsdload.c
[mirror_qemu.git] / bsd-user / bsdload.c
1 /*
2 * Load BSD executables.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "qemu/osdep.h"
19
20 #include "qemu.h"
21
22 #define TARGET_NGROUPS 32
23
24 /* ??? This should really be somewhere else. */
25 abi_long memcpy_to_target(abi_ulong dest, const void *src,
26 unsigned long len)
27 {
28 void *host_ptr;
29
30 host_ptr = lock_user(VERIFY_WRITE, dest, len, 0);
31 if (!host_ptr) {
32 return -TARGET_EFAULT;
33 }
34 memcpy(host_ptr, src, len);
35 unlock_user(host_ptr, dest, 1);
36 return 0;
37 }
38
39 static int count(char **vec)
40 {
41 int i;
42
43 for (i = 0; *vec; i++) {
44 vec++;
45 }
46
47 return i;
48 }
49
50 static int prepare_binprm(struct bsd_binprm *bprm)
51 {
52 struct stat st;
53 int mode;
54 int retval;
55
56 if (fstat(bprm->fd, &st) < 0) {
57 return -errno;
58 }
59
60 mode = st.st_mode;
61 if (!S_ISREG(mode)) { /* Must be regular file */
62 return -EACCES;
63 }
64 if (!(mode & 0111)) { /* Must have at least one execute bit set */
65 return -EACCES;
66 }
67
68 bprm->e_uid = geteuid();
69 bprm->e_gid = getegid();
70
71 /* Set-uid? */
72 if (mode & S_ISUID) {
73 bprm->e_uid = st.st_uid;
74 }
75
76 /* Set-gid? */
77 /*
78 * If setgid is set but no group execute bit then this
79 * is a candidate for mandatory locking, not a setgid
80 * executable.
81 */
82 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
83 bprm->e_gid = st.st_gid;
84 }
85
86 memset(bprm->buf, 0, sizeof(bprm->buf));
87 retval = lseek(bprm->fd, 0L, SEEK_SET);
88 if (retval >= 0) {
89 retval = read(bprm->fd, bprm->buf, 128);
90 }
91 if (retval < 0) {
92 perror("prepare_binprm");
93 exit(-1);
94 } else {
95 return retval;
96 }
97 }
98
99 /* Construct the envp and argv tables on the target stack. */
100 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
101 abi_ulong stringp, int push_ptr)
102 {
103 int n = sizeof(abi_ulong);
104 abi_ulong envp;
105 abi_ulong argv;
106
107 sp -= (envc + 1) * n;
108 envp = sp;
109 sp -= (argc + 1) * n;
110 argv = sp;
111 if (push_ptr) {
112 /* FIXME - handle put_user() failures */
113 sp -= n;
114 put_user_ual(envp, sp);
115 sp -= n;
116 put_user_ual(argv, sp);
117 }
118 sp -= n;
119 /* FIXME - handle put_user() failures */
120 put_user_ual(argc, sp);
121
122 while (argc-- > 0) {
123 /* FIXME - handle put_user() failures */
124 put_user_ual(stringp, argv);
125 argv += n;
126 stringp += target_strlen(stringp) + 1;
127 }
128 /* FIXME - handle put_user() failures */
129 put_user_ual(0, argv);
130 while (envc-- > 0) {
131 /* FIXME - handle put_user() failures */
132 put_user_ual(stringp, envp);
133 envp += n;
134 stringp += target_strlen(stringp) + 1;
135 }
136 /* FIXME - handle put_user() failures */
137 put_user_ual(0, envp);
138
139 return sp;
140 }
141
142 int loader_exec(const char *filename, char **argv, char **envp,
143 struct target_pt_regs *regs, struct image_info *infop)
144 {
145 struct bsd_binprm bprm;
146 int retval;
147 int i;
148
149 bprm.p = TARGET_PAGE_SIZE * MAX_ARG_PAGES - sizeof(unsigned int);
150 for (i = 0 ; i < MAX_ARG_PAGES ; i++) { /* clear page-table */
151 bprm.page[i] = NULL;
152 }
153 retval = open(filename, O_RDONLY);
154 if (retval < 0) {
155 return retval;
156 }
157 bprm.fd = retval;
158 bprm.filename = (char *)filename;
159 bprm.argc = count(argv);
160 bprm.argv = argv;
161 bprm.envc = count(envp);
162 bprm.envp = envp;
163
164 retval = prepare_binprm(&bprm);
165
166 if (retval >= 0) {
167 if (bprm.buf[0] == 0x7f
168 && bprm.buf[1] == 'E'
169 && bprm.buf[2] == 'L'
170 && bprm.buf[3] == 'F') {
171 retval = load_elf_binary(&bprm, regs, infop);
172 } else {
173 fprintf(stderr, "Unknown binary format\n");
174 return -1;
175 }
176 }
177
178 if (retval >= 0) {
179 /* success. Initialize important registers */
180 do_init_thread(regs, infop);
181 return retval;
182 }
183
184 /* Something went wrong, return the inode and free the argument pages*/
185 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
186 g_free(bprm.page[i]);
187 }
188 return retval;
189 }