Lazarus

Programming => Operating Systems => Linux => Topic started by: Trenatos on July 05, 2019, 12:32:09 am

Title: Set environment variable
Post by: Trenatos on July 05, 2019, 12:32:09 am
I'm working on translating a Haskell example for creating an AWS custom lambda runtime, it requires setting an environment variable but as I search I can only find references for reading environment variables and setting them for Windows.

The AWS runtimes are based on Linux.

Does anyone know how to set environment variables on Linux?
Title: Re: Set environment variable
Post by: lucamar on July 05, 2019, 01:14:57 am
Depends: for the current process or for running a secondary process? If the first, I don't know of any way to do it. For the second, just build an environment and pass it to the exec/fork call you're using.
Title: Re: Set environment variable
Post by: Trenatos on July 05, 2019, 01:35:45 am
This would be so that another process (Not controlled by me) can read the environment variable after it's been set.

I've never had to do this from code before, has always been part of various build processes, but since AWS lambda just has to be special, I'll need to.
Title: Re: Set environment variable
Post by: Kays on July 05, 2019, 01:44:00 am
[…] If the first, I don't know of any way to do it. […]
Well, setenv(3), but while there’s a baseUnix.fpGetEnv() (https://www.freepascal.org/docs-html/rtl/baseunix/fpgetenv.html) there’s no fpSetEnv. Maybe it’s on purpose, though?
Title: Re: Set environment variable
Post by: lucamar on July 05, 2019, 02:27:10 am
[…] If the first, I don't know of any way to do it. […]
Well, setenv(3), but while there’s a baseUnix.fpGetEnv() (https://www.freepascal.org/docs-html/rtl/baseunix/fpgetenv.html) there’s no fpSetEnv. Maybe it’s on purpose, though?

That's what I meant, yes, that there is no function to do it(*). One could always add it by hand, of course, but it's not there.


(*) Well, there is one setenv(); but it's in libc, which I have always thought was there strictly for Kylix compatibility.

ETA: If you're interested in adding it yourself, something like this should suffice:
Code: Pascal  [Select][+][-]
  1. function fpsetenv (name, value: pchar; overwrite: Integer): Integer; cdecl; external clib name 'setenv';
Title: Re: Set environment variable
Post by: Trenatos on July 05, 2019, 04:47:07 am
After tinkering with this a bit more, it doesn't look like I need to set the environment variable, but it would give me more details.

It's on my to-do list now, thanks guys!

No frameworks or fancy setup, I'm just cross compiling from my Mac set to a Linux x64 target, then zipping and uploading the executable, the whole process takes seconds.

And here's the result of running a pure FPC custom runtime on AWS lambda.

Init Duration: 13.68 ms
Duration: 1.58 ms
Billed Duration: 100 ms
Memory Size: 128 MB
Max Memory Used: 31 MB

Max memory is for the entire container, this little runtime doesn't do anything other than echo back whatever input it got.

For anyone else using AWS lambdas, that duration is with the lambda being warm, though cold starts are crazy fast with FPC on lambda anyway.

Next up, figuring out how to talk to AWS databases.

This will be an article series once I document the process, it really is smooth and without any of the hackery.

No side-loading, no launching through Node.js, etc. Just a compiled FPC program doing its thing.
Title: Re: Set environment variable
Post by: lucamar on July 05, 2019, 05:30:29 am
This will be an article series once I document the process, it really is smooth and without any of the hackery.

No side-loading, no launching through Node.js, etc. Just a compiled FPC program doing its thing.

Good work! Sounds quite interesting; I've added it to my list of "things to keep a watch on". :)
Title: Re: Set environment variable
Post by: Trenatos on July 05, 2019, 06:14:56 am
I did take a stab at your example but immediately run into "Cannot find clib" when adding it to the uses clause.

Would it matter that I'm on a Mac and on a *nix? (Cross compiling)
Title: Re: Set environment variable
Post by: lucamar on July 05, 2019, 10:12:08 am
It may matter but I don't really know. I've never worked on MacOS X
Title: Re: Set environment variable
Post by: marcov on July 05, 2019, 10:19:46 am
(*) Well, there is one setenv(); but it's in libc, which I have always thought was there strictly for Kylix compatibility.

Afaik setenv does not set the parents environment, only the copy that is passed to child processes.
Title: Re: Set environment variable
Post by: toby on July 25, 2019, 09:29:23 pm
what about setting it directly in the bash shell and export it ?   using aprocess.execute ?

it is just 'c'  not clib if you are wanting libc ?
Title: Re: Set environment variable
Post by: lucamar on July 25, 2019, 09:49:24 pm
what about setting it directly in the bash shell and export it ?   using aprocess.execute ?

it is just 'c'  not clib if you are wanting libc ?

That sets it only for the child bash you execute with the TProcess. It dissapears at once after the export is executed and the process ends.

That is, unless there is some option or command to make it affect the parent or master environment which I don't know about. Which it might well be: I'm very far from being a bash-guru :)
Title: Re: Set environment variable
Post by: marcov on July 26, 2019, 10:24:23 am
what about setting it directly in the bash shell and export it ?   using aprocess.execute ?

it is just 'c'  not clib if you are wanting libc ?

clib is an per platform alias. In the past there have been systems where the libc library was called differently (root, system or "glibc")
TinyPortal © 2005-2018