You are not logged in.

#1 2013-02-14 17:00:51

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Processing output of ldd native in C?

Hey,

Finally, I have implemented all the features I want in my RuneScape Client for Linux, so now it's time to tidy up the code and fix the present bugs.

The first issue I would like to tackle is the following:

In order to get OpenGL to work (on other distributions, on Arch we don't need a fix) I need to feed Java the LD_LIBRARY_PATH to libjli.so. In order to check where that file resides, I use ldd. Currently, I'm handling it like this:

ldd_command = g_strjoin(" ", "ldd", java_binary, "| awk '/libjli.so/ {print $3}' | sed 's/jli\\/libjli\\.so//'", NULL);
ldd_output = popen(ldd_command, "r");
if (ldd_output != NULL) {
	fgets(opengl_fix, sizeof(opengl_fix), ldd_output);
	pclose(ldd_output);
	ld_library_path = g_strjoin("", "LD_LIBRARY_PATH=", opengl_fix, NULL);
} else {
	g_fprintf(stderr, "Could not retrieve the path to libjli.so: Java will run without OpenGL implementation\n\n");
}

So, basically, to actually process the output I now reside on the shell. I could not find a way to process this natively in C. Do you know of one - or is this the best way to handle this?

Thanks in advance!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2 2013-02-14 17:47:46

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: Processing output of ldd native in C?

Using regular expressions makes it easy, for example:

>>> re.search(r'libjli.so => (.*)/jli/libjli.so', ldd_output).group(1)
'/usr/lib/jvm/java-7-openjdk/jre/bin/../lib/amd64'

It's in Python, but the regular expressions themselves are pretty similar between implemtations, you just have to figure a right pattern. There is a regex implementation 'pcre' in core for Perl like syntax and regex.h for POSIX regexes, i'm sure there are more.

Last edited by kaszak696 (2013-02-14 17:59:26)


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#3 2013-02-14 22:38:23

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: Processing output of ldd native in C?

That seems doable yes, although mighty complicated. I'll have to sit down and read some manuals before getting it working!

I'll report back!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#4 2013-02-15 01:08:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,422
Website

Re: Processing output of ldd native in C?

ldd = popen(ldd_command,"r");
char line[MAX_LINE+1];
char libfile[MAX_LINE] = "";
while (fgets(line,MAX_LINE,ldd) && (sscanf(line," libjli.so => %s",libfile) != 1 ));
if (strlen(libfile) > 1) {
	// do something with found libfile
}
pclose(ldd);

This assumes MAX_LINE has been suitable defined.

EDIT: oops, missed the sed command part, but that should be easy enough - I'll give you a shot at that and check back a bit later.
The double backslashes threw me for a bit - I forgot you were escaping for the passing to the shell.  So all you are doing is removing the beginning of the string?

char *newlibfil = strstr(libfile,".so")+3;

But in looking back, what should this return?  That removes most of the ldd output.

EDIT2: Actually, I'd just ditch the popen("ldd ...") all together and just do what ldd does: read from the binary header.  ldd is very small to start with, and a lot of the code is checking to make sure it was actually passed a suitable parameter: http://src.gnu-darwin.org/src/usr.bin/ldd/ldd.c.html

Last edited by Trilby (2013-02-15 01:45:59)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2013-02-15 11:27:25

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: Processing output of ldd native in C?

Trilby wrote:

But in looking back, what should this return?  That removes most of the ldd output.

In order to get OpenGL working on other distributions, I have to feed Java the location of its libraries.
Running ldd gives me this:

libjli.so => /usr/lib/jvm/java-7-openjdk/jre/bin/../lib/amd64/jli/libjli.so (0x00007feabafc4000)

I need to feed that to Java like this:

LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk/jre/bin/../lib/amd64

So yea, I need to remove most of the output.

Regarding ditching all of what I have now, I tried that. On one of my Google results, they mentioned I could get ldd-like functionality by using this:

setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1);

Problem is, the output still is the same so I'm still struggling with how to process that. I will take a look at your suggestions after work!

Thanks!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#6 2013-02-15 11:58:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,422
Website

Re: Processing output of ldd native in C?

If you do call ldd with popen my suggestions up to the part the replicat the sed command should work.  The sed command can be replicated instead by the following:

*strstr(libfile,"/jli/libjli.so") = '\0';

This truncates the string at the start of that substring.

This could cause problems though if something went wrong and there was no match, so I'd include the following check

char *endpoint = strstr(libflile,"/jli/libjli.so");
if (endpoint) {
   endpoint = '\0';
   // do something with libfile
}
else {
   // error
}

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2013-02-15 14:47:23

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Processing output of ldd native in C?

Unia wrote:

I need to feed Java the LD_LIBRARY_PATH to libjli.so

This really seems like something that needs to be fixed in /etc/ld.so.conf.d/ so that the library can be loaded via normal means...

I really think you're solving the wrong problem, but you haven't really explained enough about what the problem is.

Offline

#8 2013-02-15 14:57:14

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: Processing output of ldd native in C?

falconindy wrote:
Unia wrote:

I need to feed Java the LD_LIBRARY_PATH to libjli.so

This really seems like something that needs to be fixed in /etc/ld.so.conf.d/ so that the library can be loaded via normal means...

I really think you're solving the wrong problem, but you haven't really explained enough about what the problem is.

On Arch Linux it works just fine, but I guess other distributions do not configure Java properly: when I start the game there, OpenGL can not be used (if it could be, it would be used by default by RuneScape, but it's not and when I change the setting, it crashes). Thus, to get OpenGL working on those distributions, I have to tell Java where to find the libraries.

Last edited by Unia (2013-02-15 14:57:38)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#9 2013-02-15 18:11:09

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Processing output of ldd native in C?

ld.so.conf.d isn't Arch specific. I'm still very unconvinced you're doing it right; especially so if you say that other distributions are configuring java incorrectly.

Offline

#10 2013-02-15 18:55:44

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: Processing output of ldd native in C?

falconindy wrote:

ld.so.conf.d isn't Arch specific. I'm still very unconvinced you're doing it right; especially so if you say that other distributions are configuring java incorrectly.

Not being able to use OpenGL by default seems incorrectly setup, no? I mean, here on Arch it works out of the box so why not on other distributions? However, I have no knowledge on the matter. My client is based on another one that's written in Perl and that developer is my main contact for when I had questions. He seems to know his stuff (and he is in contact with the developers of RuneScape) so I thought he would probably be right. I asked him on Skype today when you mentioned /etc/ld.so.conf.d/ and here is his reply:

[16:28:57] HikariKnight: true but i decided to not mess with those because java hardlinks them (at least in java6)
plus with the LD_LIBRARY_PATH i have an universal way :P plus i can support custom javas :P
[16:29:54] Unia: universal way how?
[16:30:30] HikariKnight: 2 scenarios :P
[16:31:46] HikariKnight: 1. a linux distro decide to play king of the hill and fuck with standards (fedora loves to do this sometimes)
2. ld.conf.d is changed
3. works on solaris and bsd too
[16:31:49] HikariKnight: and ofcourse
[16:31:58] HikariKnight: we can use custom java for runescape itself
[16:32:28] HikariKnight: lets say that a user NEEDs to use oracle java however rs runs better with openjdk (or other way  around)
[16:32:46] HikariKnight: so client is able to use a different java without screwing up the ld.conf ;)

However, I am very willing to change my current approach - could you explain a bit more about this ld.so.conf.d? It seems a better method of fixing this.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#11 2013-02-16 13:58:30

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: Processing output of ldd native in C?

I have looked more into ld.so.conf but I'm still failing to see why this would be an improvement: If I were to make /etc/ld.so.conf.d/java.conf, I would still have to somehow figure out the path to libjli.so because this is different between distributions and even Oracle or OpenJDK.

Falconindy, can you elaborate on your approach?


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

Board footer

Powered by FluxBB