4632N/A/*
4632N/A * Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#include <stdlib.h>
4632N/A#include <unistd.h>
4632N/A#include <string.h>
5089N/A#include <ctype.h>
4632N/A#include "sys.h"
4632N/A#include "util.h"
4632N/A
4632N/A#if defined(LINUX) || defined(_ALLBSD_SOURCE)
4632N/A /* Linux */
4632N/A #define FORK() fork()
4632N/A#else
4632N/A /* Solaris (make sure we always get the POSIX-specified behavior) */
4632N/A #define FORK() fork1()
4632N/A#endif
4632N/A
4632N/Astatic char *skipWhitespace(char *p) {
4632N/A while ((*p != '\0') && isspace(*p)) {
4632N/A p++;
4632N/A }
4632N/A return p;
4632N/A}
4632N/A
4632N/Astatic char *skipNonWhitespace(char *p) {
4632N/A while ((*p != '\0') && !isspace(*p)) {
4632N/A p++;
4632N/A }
4632N/A return p;
4632N/A}
4632N/A
4632N/Aint
4632N/AdbgsysExec(char *cmdLine)
4632N/A{
4632N/A int i;
4632N/A int argc;
4632N/A pid_t pid_err = (pid_t)(-1); /* this is the error return value */
4632N/A pid_t pid;
4632N/A char **argv = NULL;
4632N/A char *p;
4632N/A char *args;
4632N/A
4632N/A /* Skip leading whitespace */
4632N/A cmdLine = skipWhitespace(cmdLine);
4632N/A
4632N/A /*LINTED*/
4632N/A args = jvmtiAllocate((jint)strlen(cmdLine)+1);
4632N/A if (args == NULL) {
4632N/A return SYS_NOMEM;
4632N/A }
4632N/A (void)strcpy(args, cmdLine);
4632N/A
4632N/A p = args;
4632N/A
4632N/A argc = 0;
4632N/A while (*p != '\0') {
4632N/A p = skipNonWhitespace(p);
4632N/A argc++;
4632N/A if (*p == '\0') {
4632N/A break;
4632N/A }
4632N/A p = skipWhitespace(p);
4632N/A }
4632N/A
4632N/A /*LINTED*/
4632N/A argv = jvmtiAllocate((argc + 1) * (jint)sizeof(char *));
4632N/A if (argv == 0) {
4632N/A jvmtiDeallocate(args);
4632N/A return SYS_NOMEM;
4632N/A }
4632N/A
4632N/A for (i = 0, p = args; i < argc; i++) {
4632N/A argv[i] = p;
4632N/A p = skipNonWhitespace(p);
4632N/A *p++ = '\0';
4632N/A p = skipWhitespace(p);
4632N/A }
4632N/A argv[i] = NULL; /* NULL terminate */
4632N/A
4632N/A if ((pid = FORK()) == 0) {
4632N/A /* Child process */
4632N/A int i;
4632N/A long max_fd;
4632N/A
4632N/A /* close everything */
4632N/A max_fd = sysconf(_SC_OPEN_MAX);
4632N/A /*LINTED*/
4632N/A for (i = 3; i < (int)max_fd; i++) {
4632N/A (void)close(i);
4632N/A }
4632N/A
4632N/A (void)execvp(argv[0], argv);
4632N/A
4632N/A exit(-1);
4632N/A }
4632N/A jvmtiDeallocate(args);
4632N/A jvmtiDeallocate(argv);
4632N/A if (pid == pid_err) {
4632N/A return SYS_ERR;
4632N/A } else {
4632N/A return SYS_OK;
4632N/A }
4632N/A}
4632N/A