Annotation of prex/usr/server/exec/args.c, Revision 1.1
1.1 ! nbrk 1: /*
! 2: * Copyright (c) 2005-2007, Kohsuke Ohtani
! 3: * All rights reserved.
! 4: *
! 5: * Redistribution and use in source and binary forms, with or without
! 6: * modification, are permitted provided that the following conditions
! 7: * are met:
! 8: * 1. Redistributions of source code must retain the above copyright
! 9: * notice, this list of conditions and the following disclaimer.
! 10: * 2. Redistributions in binary form must reproduce the above copyright
! 11: * notice, this list of conditions and the following disclaimer in the
! 12: * documentation and/or other materials provided with the distribution.
! 13: * 3. Neither the name of the author nor the names of any co-contributors
! 14: * may be used to endorse or promote products derived from this software
! 15: * without specific prior written permission.
! 16: *
! 17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 27: * SUCH DAMAGE.
! 28: */
! 29:
! 30: /*
! 31: * args.c - routine to build arguments
! 32: */
! 33:
! 34: #include <prex/prex.h>
! 35: #include <server/fs.h>
! 36: #include <server/proc.h>
! 37: #include <sys/list.h>
! 38:
! 39: #include <limits.h>
! 40: #include <unistd.h>
! 41: #include <stdlib.h>
! 42: #include <string.h>
! 43: #include <stdio.h>
! 44: #include <fcntl.h>
! 45: #include <unistd.h>
! 46: #include <errno.h>
! 47:
! 48: #include "exec.h"
! 49:
! 50: /*
! 51: * Build argument on stack.
! 52: *
! 53: * Stack layout:
! 54: * file name string
! 55: * env string
! 56: * arg string
! 57: * NULL
! 58: * envp[n]
! 59: * NULL
! 60: * argv[n]
! 61: * argc
! 62: *
! 63: * NOTE: This may depend on processor architecture.
! 64: */
! 65: int
! 66: build_args(task_t task, void *stack, struct exec_msg *msg, void **new_sp)
! 67: {
! 68: int argc, envc;
! 69: char *path, *file;
! 70: char **argv, **envp;
! 71: int i, err;
! 72: u_long arg_top, mapped, sp;
! 73:
! 74: argc = msg->argc;
! 75: envc = msg->envc;
! 76: path = (char *)&msg->path;
! 77:
! 78: /*
! 79: * Map target stack in current task.
! 80: */
! 81: err = vm_map(task, stack, USTACK_SIZE, (void *)&mapped);
! 82: if (err)
! 83: return ENOMEM;
! 84: memset((void *)mapped, 0, USTACK_SIZE);
! 85:
! 86: sp = mapped + USTACK_SIZE - sizeof(int) * 3;
! 87:
! 88: /*
! 89: * Copy items
! 90: */
! 91:
! 92: /* File name */
! 93: *(char *)sp = '\0';
! 94: sp -= strlen(path);
! 95: sp = ALIGN(sp);
! 96: strlcpy((char *)sp, path, USTACK_SIZE);
! 97: file = (char *)sp;
! 98:
! 99: /* arg/env */
! 100: sp -= ALIGN(msg->bufsz);
! 101: memcpy((char *)sp, (char *)&msg->buf, msg->bufsz);
! 102: arg_top = sp;
! 103:
! 104: /* envp[] */
! 105: sp -= ((envc + 1) * sizeof(char *));
! 106: envp = (char **)sp;
! 107:
! 108: /* argv[] */
! 109: sp -= ((argc + 1) * sizeof(char *));
! 110: argv = (char **)sp;
! 111:
! 112: /* argc */
! 113: sp -= sizeof(int);
! 114: *(int *)(sp) = argc + 1;
! 115:
! 116: /*
! 117: * Build argument list
! 118: */
! 119: argv[0] = (char *)((u_long)stack + (u_long)file - mapped);
! 120: DPRINTF(("exec: argv[0] = %s\n", argv[0]));
! 121:
! 122: for (i = 1; i <= argc; i++) {
! 123: argv[i] = (char *)((u_long)stack + (arg_top - mapped));
! 124: while ((*(char *)arg_top++) != '\0');
! 125: DPRINTF(("exec: argv[%d] = %s\n", i, argv[i]));
! 126: }
! 127: argv[argc + 1] = NULL;
! 128:
! 129: for (i = 0; i < envc; i++) {
! 130: envp[i] = (char *)((u_long)stack + (arg_top - mapped));
! 131: while ((*(char *)arg_top++) != '\0');
! 132: }
! 133: envp[envc] = NULL;
! 134:
! 135: *new_sp = (void *)((u_long)stack + (sp - mapped));
! 136: vm_free(task_self(), (void *)mapped);
! 137:
! 138: return 0;
! 139: }
CVSweb