Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/back/exec_md.c
32285 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <unistd.h>27#include <string.h>28#include <ctype.h>29#include "jni.h"30#include "sys.h"31#include "util.h"3233#if defined(LINUX) || defined(_ALLBSD_SOURCE) || defined(AIX)34/* Linux, BSD, AIX */35#define FORK() fork()36#else37/* Solaris (make sure we always get the POSIX-specified behavior) */38#define FORK() fork1()39#endif4041static char *skipWhitespace(char *p) {42while ((*p != '\0') && isspace(*p)) {43p++;44}45return p;46}4748static char *skipNonWhitespace(char *p) {49while ((*p != '\0') && !isspace(*p)) {50p++;51}52return p;53}5455int56dbgsysExec(char *cmdLine)57{58int i;59int argc;60pid_t pid_err = (pid_t)(-1); /* this is the error return value */61pid_t pid;62char **argv = NULL;63char *p;64char *args;6566/* Skip leading whitespace */67cmdLine = skipWhitespace(cmdLine);6869/*LINTED*/70args = jvmtiAllocate((jint)strlen(cmdLine)+1);71if (args == NULL) {72return SYS_NOMEM;73}74(void)strcpy(args, cmdLine);7576p = args;7778argc = 0;79while (*p != '\0') {80p = skipNonWhitespace(p);81argc++;82if (*p == '\0') {83break;84}85p = skipWhitespace(p);86}8788/*LINTED*/89argv = jvmtiAllocate((argc + 1) * (jint)sizeof(char *));90if (argv == 0) {91jvmtiDeallocate(args);92return SYS_NOMEM;93}9495for (i = 0, p = args; i < argc; i++) {96argv[i] = p;97p = skipNonWhitespace(p);98*p++ = '\0';99p = skipWhitespace(p);100}101argv[i] = NULL; /* NULL terminate */102103if ((pid = FORK()) == 0) {104/* Child process */105int i;106long max_fd;107108/* close everything */109max_fd = sysconf(_SC_OPEN_MAX);110/*LINTED*/111for (i = 3; i < (int)max_fd; i++) {112(void)close(i);113}114115(void)execvp(argv[0], argv);116117exit(-1);118}119jvmtiDeallocate(args);120jvmtiDeallocate(argv);121if (pid == pid_err) {122return SYS_ERR;123} else {124return SYS_OK;125}126}127128129