amd64/132677: error calling fork execvp

rzo rzo at gmx.de
Sun Mar 22 02:41:28 PDT 2009


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 <unistd.h>
>> #include <stdio.h>
>> #include <sys/types.h>
>> #include <sys/wait.h>
>> #include <stdlib.h>
>>
>> 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.
>>
>
>



More information about the freebsd-java mailing list