Dennis Portello wrote:
Hi,
I've been experimenting with JNA over the last few days to gain access
to POSIX functions on Linux/Unix.
So far I've had success with calling setuid when running Apache Tomcat
so I can start as root and move to a non-privileged account during
runtime.
For my next trick, I'd like to write a signal handler so I can
initiate a proper shutdown when a SIGINT or SIGHUP is caught.
My dilemma is this, I was in the process of implementing the sigalert
struct in java when I came across a union. I've looked around the jna
code, examples on the site, and whatever emails I could find and found
nothing regarding this issue.
I couldn't find the sigalert structure anywhere. Did you mean struct
sigaction, with its union of function pointers?
Assuming sigaction, there currently isn't any way to set function
pointers in a structure anyway - so using the old signal(3) call might
be a better bet to hook signal handlers for now.
Could someone provide insight as to how I might handle a c union with
JNA, or is this something that's not currently possible?
There is no easy way to do it currently. You can always point multiple
structures at the same memory region.
e.g.
class Foo extends Structure {
public int i1;
public int i2;
public Foo(Pointer p) {
useMemory(p);
}
}
class Bar extends Structure {
public long l;
public Bar(Pointer p) {
useMemory(p);
}
}
Pointer p = new Memory(...)
Foo f = new Foo(p);
foo.i1 = 0xdeadbeef;
foo.i2 = 0xcafebabe;
foo.write();
Bar b = new Bar(p);
b.read();
System.out.println("l=" + b.l);