Requesting help with C extension

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Requesting help with C extension

Simon Wunderlin
Dear lua developers and users

I recently decided to embed Lua into a C Application. The requirements are:
- minimal dependencies
- Linux and later windows (mingw) and OSX

For my application to work I need to set environment variables. Therefore I have written a simple Lua C extensions to do so: https://pastebin.com/NsNMHkUu

The Extension is used in Lua as follows: https://pastebin.com/YquT0Rrf

Now if I run the program, the following is happening:
 1. in the os_setenv() C function, I can successfully read back the previously set env
 2. when I try to read it back in Lua, i get garbage data as if I would have messed up with a pointer
 3. when I try to use my C function os_getenv() from the extension, I get NULL as a result.

Here is the output of the program:
-- bash output
wunderlins@wus-desktop5:~/Projects/protohand-lua/src$ make test
./protohand2.lua "mach://gedit?f=~/.profile&param1=value+1&param%202=value2&user=wus" "example.ini"
PH_HOME: /home/wunderlins/Projects/protohand-lua/src/
env (0): PH_HOME=/home/wunderlins/Projects/protohand-lua/src/, /home/wunderlins/Projects/protohand-lua/src/
� A
name: PH_HOME
env: (null)
nil unable to get environment variable 'PH_HOME'
-- end bash output

The first 2 lines of output come fro mthe C extension. 1st line from os_setenv, 2nd line from os_getenv (in my extension) and the rest is from the Lua script.

What is the mistake I am making? Any help is greatly appreciated.

My environment:
- Debian 8, x64
- gcc 4.9
- Lua 5.3.5 (compiled from source with default options and custom --prefix=)

Kind regards
-S

Reply | Threaded
Open this post in threaded view
|

Re: Requesting help with C extension

Andrew Gierth
>>>>> "Simon" == Simon Wunderlin <[hidden email]> writes:

 Simon> For my application to work I need to set environment variables.
 Simon> Therefore I have written a simple Lua C extensions to do so:
 Simon> https://pastebin.com/NsNMHkUu

putenv() is dangerous; if your OS has setenv() you should use that
instead.

The problem with putenv() is that it does not copy the supplied string,
it just puts a pointer to it into the environment. setenv(), in
contrast, makes a copy of the supplied string.

--
Andrew.

Reply | Threaded
Open this post in threaded view
|

Re: Requesting help with C extension

Viacheslav Usov
On Fri, Jul 20, 2018 at 8:07 AM Andrew Gierth <[hidden email]> wrote:

> The problem with putenv() is that it does not copy the supplied string, it just puts a pointer to it into the environment.

"Thus, it is an error is to call putenv() with an automatic variable as the argument, then return from the calling function while string is still part of the environment." [1] Which is exactly what the posted code is doing.

Cheers,
V.

Reply | Threaded
Open this post in threaded view
|

Re: Requesting help with C extension

pocomane
On Fri, Jul 20, 2018 at 7:53 AM Simon Wunderlin <[hidden email]> wrote:
>
> Dear lua developers and users
>
> I recently decided to embed Lua into a C Application. The requirements are:
> - minimal dependencies
> - Linux and later windows (mingw) and OSX
>
> For my application to work I need to set environment variables. Therefore I have written a simple Lua C extensions to do so: https://pastebin.com/NsNMHkUu

Maybe a bit offtopic, but...

If you want to set env variables, probably you also want to spawn
sub-process, and maybe read/write its output/input. Luachild [1]
module can do all of this for you, and nothing else. It is "Minimal"
code extracted from the Ex module of LuaDist. It uses setenv for
setting variables, or SetEnvironmentVariable on windows.

Moreover, there are some alternatives listed in the readme.

[1] https://github.com/pocomane/luachild - MIT license - Credit to the
original author, Mark Edgar.

Reply | Threaded
Open this post in threaded view
|

Re: Requesting help with C extension

Simon Wunderlin
Dear Andre, Viacheslav and pocomane

Since I need it for linux/mingw/osx, I can use setenv (which works). If I wasn't crap at reading man pages I could have figured that one out myself.

Thank you for your quick reply, kind regards,
-S


On Fri, Jul 20, 2018 at 9:20 AM, pocomane <[hidden email]> wrote:
On Fri, Jul 20, 2018 at 7:53 AM Simon Wunderlin <[hidden email]> wrote:
>
> Dear lua developers and users
>
> I recently decided to embed Lua into a C Application. The requirements are:
> - minimal dependencies
> - Linux and later windows (mingw) and OSX
>
> For my application to work I need to set environment variables. Therefore I have written a simple Lua C extensions to do so: https://pastebin.com/NsNMHkUu

Maybe a bit offtopic, but...

If you want to set env variables, probably you also want to spawn
sub-process, and maybe read/write its output/input. Luachild [1]
module can do all of this for you, and nothing else. It is "Minimal"
code extracted from the Ex module of LuaDist. It uses setenv for
setting variables, or SetEnvironmentVariable on windows.

Moreover, there are some alternatives listed in the readme.

[1] https://github.com/pocomane/luachild - MIT license - Credit to the
original author, Mark Edgar.




--
Insanity: doing the same thing over and over again and expecting different results.
Reply | Threaded
Open this post in threaded view
|

Re: Requesting help with C extension

Simon Wunderlin
In reply to this post by pocomane
Hi pocomane

On 07/20/2018 09:20 AM, pocomane wrote:
...

> Maybe a bit offtopic, but...
>
> If you want to set env variables, probably you also want to spawn
> sub-process, and maybe read/write its output/input. Luachild [1]
> module can do all of this for you, and nothing else. It is "Minimal"
> code extracted from the Ex module of LuaDist. It uses setenv for
> setting variables, or SetEnvironmentVariable on windows.
>
> Moreover, there are some alternatives listed in the readme.
>
> [1] https://github.com/pocomane/luachild - MIT license - Credit to the
> original author, Mark Edgar.


You have guessed correctly, you code is much appreciated!


Kind regards
-S