From owner-freebsd-java@FreeBSD.ORG Sun Mar 22 09:41:27 2009 Return-Path: Delivered-To: freebsd-java@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A8B21106564A for ; Sun, 22 Mar 2009 09:41:27 +0000 (UTC) (envelope-from rzo@gmx.de) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mx1.freebsd.org (Postfix) with SMTP id 282D78FC15 for ; Sun, 22 Mar 2009 09:41:26 +0000 (UTC) (envelope-from rzo@gmx.de) Received: (qmail invoked by alias); 22 Mar 2009 09:14:45 -0000 Received: from p54A322F6.dip0.t-ipconnect.de (EHLO [192.168.0.90]) [84.163.34.246] by mail.gmx.net (mp011) with SMTP; 22 Mar 2009 10:14:45 +0100 X-Authenticated: #1178861 X-Provags-ID: V01U2FsdGVkX18Rmzx66OYRdwKxLqNVET8VvFUnihCW8KQAar6TIB zA9xQj0z9tV9xz Message-ID: <49C60185.8020006@gmx.de> Date: Sun, 22 Mar 2009 10:14:45 +0100 From: rzo User-Agent: Thunderbird 2.0.0.21 (Windows/20090302) MIME-Version: 1.0 To: freebsd-java@FreeBSD.org References: <200903152251.n2FMpnRk014352@www.freebsd.org> <49BF3736.2040308@gmx.de> In-Reply-To: <49BF3736.2040308@gmx.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.44 Cc: Subject: Re: amd64/132677: error calling fork execvp X-BeenThere: freebsd-java@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Porting Java to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Mar 2009 09:41:28 -0000 Hello, I am having problems calling fork/exec from within java on FreeBSD 64. I would appreciate if you could help me with this. - Ron http://yajsw.sourceforge.net/ ------------------ "Do you have specific reason to believe that this usage of fork is supported by Java? " Well, java has the ability to spwan a process (Runtime.exec()), so it can fork/exec. The problem with this solution is that the spawned process is not demonized, whereas I need to spawn a daemon. My program works fine on FreeBSD 32bit. Therefore my assumption that this is a FreeBSD amd64 issue. Maybe I should check with the freebsd java team. Thanks Ron ------------------------- Nate Eldredge wrote: > On Sun, 22 Mar 2009, rzo wrote: > >> Nate, >> >> I have tried calling the execvp with a null as last argument. But it >> will not work. >> Here the code I am using: >> >> you will need this library to compile it. >> >> https://jna.dev.java.net/source/browse/*checkout*/jna/trunk/jnalib/dist/jna.jar?rev=HEAD >> > > Thanks for the details. I'm not sure that this is a FreeBSD bug. The > program does indeed exhibit weird behavior in the child process, but I > think this is more likely due to the Java virtual machine not > expecting to be forked. Do you have specific reason to believe that > this usage of fork is supported by Java? > > I am not a Java expert, so I can't be sure, but I would recommend > asking some Java experts. In particular, I have the impression that > this is not the recommended way to spawn a new program from Java. > -------------------------- PS: I am using the following JDK: diablo-jdk-freebsd6.amd64.1.6.0.07.02.tbz - Ron -------------- Nate, I have tried calling the execvp with a null as last argument. But it will not work. Here the code I am using: you will need this library to compile it. https://jna.dev.java.net/source/browse/*checkout*/jna/trunk/jnalib/dist/jna.jar?rev=HEAD package test; import java.io.IOException; import com.sun.jna.Library; import com.sun.jna.Native; public class ForkTest { interface CLibrary extends Library { CLibrary INSTANCE = (CLibrary) Native.loadLibrary("c", CLibrary.class); int fork(); int execve(String path, String[] argv, String[] envp); int execvp(String filename, String[] argv); String strerror (int errnum); } public static void main(String[] args) throws IOException, InterruptedException { long pid = CLibrary.INSTANCE.fork(); if (pid == 0) { //int res = CLibrary.INSTANCE.execve("/sbin/ping", new String[]{"/sbin/ping", "localhost"}, new String[0]); int res = CLibrary.INSTANCE.execvp("/sbin/ping", new String[]{"/sbin/ping", "localhost", null}); int err = Native.getLastError(); System.out.println("errno " + err + " "+CLibrary.INSTANCE.strerror(err)); System.out.println("exec res "+res); } else { System.out.println("pid "+pid); Thread.sleep(100000); } } } ---------------------- Nate Eldredge wrote: > On Tue, 17 Mar 2009, rzo wrote: > >> Nate, >> >> I am programming in java and using jna (https://jna.dev.java.net/) to >> call libc. > > A Java source file would be fine too. > >> I have no c/c++ dev environment. > > gcc is included with FreeBSD... > >> I think the difference is that I am using { "echo", "Hello, world!"}; > > For the C function, it is necessary to have a NULL at the end of the > array. If you're missing one, that is probably the cause of the problem. > >> I will try this out on the weekend and will get back to you. > > Ok. > rzo wrote: > Nate, > > I am programming in java and using jna (https://jna.dev.java.net/) to > call libc. > I have no c/c++ dev environment. > I think the difference is that I am using { "echo", "Hello, world!"}; > I will try this out on the weekend and will get back to you. > > Regards > > - Ron > http://yajsw.sourceforge.net/ > > > Nate Eldredge wrote: >> On Sun, 15 Mar 2009, ron wrote: >> >>> hello, >>> >>> calling execvp(2) works fine. >>> >>> however calling >>> >>> pid = fork() >>> if (pid == 0) >>> execvp(2) >>> >>> returns error 22 (invalid value) >>> >>> calling >>> >>> pid = fork() >>> if (pid == 0) >>> execve(3) >>> >>> works fine >> >> Can you be more specific? In particular, could you send a complete >> source that can be compiled and run, and that shows the problem? >> >> I can't reproduce a problem here. I am running 7.1 on amd64, and the >> following program works as expected. >> >> #include >> #include >> #include >> #include >> #include >> >> int main(void) { >> pid_t pid; >> pid = fork(); >> if (pid == 0) { >> char *argv[] = { "echo", "Hello, world!", NULL }; >> execvp("echo", argv); >> perror("execvp failed"); >> _exit(1); >> } else if (pid > 0) { >> int status; >> if (waitpid(pid, &status, 0) == -1) { >> perror("waitpid"); >> exit(1); >> } >> if (WIFEXITED(status)) >> printf("Child exited with status %d\n", WEXITSTATUS(status)); >> else if (WIFSIGNALED(status)) >> printf("Child killed by signal %d\n", WTERMSIG(status)); >> else >> printf("Impossible status!\n"); >> } else if (pid < 0) { >> perror("fork"); >> exit(1); >> } >> return 0; >> } >> >> The output is: >> Hello, world! >> Child exited with status 0 >> >> Perhaps there is something wrong in a different part of your test >> program. >> > >