Dynamic loading of C functions: Badly stuck

Started by Jasbinder Balialmost 20 years ago8 messagesgeneral
Jump to latest
#1Jasbinder Bali
jsbali@gmail.com

Hi,
I raised this problem yesterday aswell. I'm badly stuck at this point.
The problem is as follows:

I have a C function that i want to use in my postgres function.
I adopt the following steps to do that.

--- compile the C file as follows
     gcc -shared -o test_func.so test_func.c
     test_func.c is the name of the C file
--- the name of the function that i want to use from this c file is called
'command'
--- Postgres function is written as follows:

CREATE FUNCTION command(integer) RETURNS integer
AS 'usr/include/pgsql/server/test_func', 'command'
LANGUAGE C STRICT;

when i try to run this function, always gives me the follwoing error:

ERROR: could not access file "usr/include/pgsql/server/test_func": No such
file or directory

I tried changin the permission of the file to 666 and even tried it with 755
but in vein.

I checked the log file but it just prints the above error and doesn't give
me any more information.

I have no clue why is postgres not reading test_func object file.

Any kind of help would be appreciated

Thanks,
~Jas

#2Joe Conway
mail@joeconway.com
In reply to: Jasbinder Bali (#1)
Re: Dynamic loading of C functions: Badly stuck

Jasbinder Bali wrote:

CREATE FUNCTION command(integer) RETURNS integer
AS 'usr/include/pgsql/server/test_func', 'command'
LANGUAGE C STRICT;

when i try to run this function, always gives me the follwoing error:

ERROR: could not access file "usr/include/pgsql/server/test_func": No
such file or directory

Should 'usr/include/pgsql/server/test_func' actually be
'/usr/include/pgsql/server/test_func'?

Note the leading '/'

HTH,

Joe

#3Jasbinder Bali
jsbali@gmail.com
In reply to: Joe Conway (#2)
Re: Dynamic loading of C functions: Badly stuck

Yes, that helped. I was missing that leading '/'

Now the error is different. It cries something on the permissions.

ERROR: could not load library "/usr/include/pgsql/server/test.so":
/usr/include/pgsql/server/test.so: failed to map segment from shared object:
Permission denied

Can you comment on this?

Thanks,
~Jas

Show quoted text

On 6/21/06, Joe Conway <mail@joeconway.com> wrote:

Jasbinder Bali wrote:

CREATE FUNCTION command(integer) RETURNS integer
AS 'usr/include/pgsql/server/test_func', 'command'
LANGUAGE C STRICT;

when i try to run this function, always gives me the follwoing error:

ERROR: could not access file "usr/include/pgsql/server/test_func": No
such file or directory

Should 'usr/include/pgsql/server/test_func' actually be
'/usr/include/pgsql/server/test_func'?

Note the leading '/'

HTH,

Joe

#4Steve Atkins
steve@blighty.com
In reply to: Jasbinder Bali (#1)
Re: Dynamic loading of C functions: Badly stuck

On Jun 21, 2006, at 9:42 AM, Jasbinder Bali wrote:

Hi,
I raised this problem yesterday aswell. I'm badly stuck at this point.
The problem is as follows:

I have a C function that i want to use in my postgres function.
I adopt the following steps to do that.

--- compile the C file as follows
gcc -shared -o test_func.so test_func.c
test_func.c is the name of the C file
--- the name of the function that i want to use from this c file is  
called 'command'
--- Postgres function is written as follows:

CREATE FUNCTION command(integer) RETURNS integer
AS 'usr/include/pgsql/server/test_func', 'command'
LANGUAGE C STRICT;

when i try to run this function, always gives me the follwoing error:

ERROR: could not access file "usr/include/pgsql/server/test_func":
No such file or directory

I tried changin the permission of the file to 666 and even tried it
with 755 but in vein.

I checked the log file but it just prints the above error and
doesn't give me any more information.

I have no clue why is postgres not reading test_func object file.

Any kind of help would be appreciated

IIRC the path name is relative to... dynamic_library_path and pwd, first
as given, then with ".so" appended.

Unless you've set one of those to "/" then 'usr/include/pgsql/server/
test_func'
is never going to resolve to where you want it to.

If you really want to keep it where it is, try using the correct
absolute filename.
Better, though, would be to use ... AS '$libdir/test_func.so' ... and
put the library
wherever "pg_config --pkglibdir" says - probably /usr/local/pgsql/lib.

Cheers,
Steve

#5Joe Conway
mail@joeconway.com
In reply to: Jasbinder Bali (#3)
Re: Dynamic loading of C functions: Badly stuck

Jasbinder Bali wrote:

Now the error is different. It cries something on the permissions.

ERROR: could not load library "/usr/include/pgsql/server/test.so":
/usr/include/pgsql/server/test.so: failed to map segment from shared
object: Permission denied

Can you comment on this?

What does
ls -l /usr/include/pgsql/server/test.so
show?

Does the postgres user (or whomever postgres is running as) have the
ability to read the file?

Joe

#6Jasbinder Bali
jsbali@gmail.com
In reply to: Joe Conway (#5)
Re: Dynamic loading of C functions: Badly stuck

well as of now my postgres is running on a trusted connection that well as
of now my postgres is running on a trusted connection that i've specified in
pg_hba.conf file.

ls -l /usr/include/pgsql/server/test.so shows the following

-rw-rw-rw- 1 root root 4620 Jun 21 12:00 /usr/include/pgsql/server/test.so

This means that the owner of this .so file is root and group is root.

Thanks
~Jas

Show quoted text

On 6/21/06, Joe Conway <mail@joeconway.com> wrote:

Jasbinder Bali wrote:

Now the error is different. It cries something on the permissions.

ERROR: could not load library "/usr/include/pgsql/server/test.so":
/usr/include/pgsql/server/test.so: failed to map segment from shared
object: Permission denied

Can you comment on this?

What does
ls -l /usr/include/pgsql/server/test.so
show?

Does the postgres user (or whomever postgres is running as) have the
ability to read the file?

Joe

#7Jasbinder Bali
jsbali@gmail.com
In reply to: Joe Conway (#5)
Re: Dynamic loading of C functions: Badly stuck

I've disabled my SELinux and now postgres is being able to access the shared
library i.e test.so file.
Don't know if thats the right way to do it or not.

PS: I'm using Fedora core 2 OS

Thanks,
~Jas

Show quoted text

On 6/21/06, Joe Conway <mail@joeconway.com> wrote:

Jasbinder Bali wrote:

Now the error is different. It cries something on the permissions.

ERROR: could not load library "/usr/include/pgsql/server/test.so":
/usr/include/pgsql/server/test.so: failed to map segment from shared
object: Permission denied

Can you comment on this?

What does
ls -l /usr/include/pgsql/server/test.so
show?

Does the postgres user (or whomever postgres is running as) have the
ability to read the file?

Joe

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jasbinder Bali (#7)
Re: Dynamic loading of C functions: Badly stuck

"Jasbinder Bali" <jsbali@gmail.com> writes:

I've disabled my SELinux and now postgres is being able to access the shared
library i.e test.so file.
Don't know if thats the right way to do it or not.

It's not. Almost certainly, SELinux is keying the rejection off the
fact that you have the .so file in the wrong place, ie, not a place that
postgres is supposed to be reading executables from.

Put it in $libdir and everything will be much better. (You might also
need to run restorecon on it, not sure.) "pg_config --pkglibdir" will
tell you where that is.

regards, tom lane