How to set an environment variable for the current process at runtime? lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- Manlio · 2022-02-13 03:29 The following code lists the environment variables of the current process: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for i := 0 to GetEnvironmentVariableCount-1 do    Log(GetEnvironmentString(i));  The result is something like this:                                                          Code: [Select]TMPDIR=/var/folders/mp/gx458m3x0c5__tsxrgcczs880000gn SHELL=/bin/bash HOME=/Users/admin ... and several more lines like that Now, I would like to add one more environmental variable to the list, e.g. MYNAME=my-value I only want to add this environmental variable to the current running process. This is useful, for example, to pass information to a dylib.(1) On Windows this can be done with SetEnvironmentVariable() and on Linux, I read, there is SetEnv(). So the question is, how do I do it on MacOS? I know about the "export" bash command, so I tried to execute the command from within my program, like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uses unix;   fpsystem('export MYNAME=my-value');  After doing that, however, if I use again GetEnvironmentVariableCount() and GetEnvironmentString(i), nothing has changed, my name/value pair is not there. So, can someone kindly show me how to do it? (1) I'm asking this question because I'm trying to use the VLC library on MacOS, and before I can call certain functions in the dylib files, I must provide an environment variable named VLC_PLUGIN_PATH containing the full path of the plugins folder. ---------------------------------------------------------------------- dbannon · 2022-02-13 04:40 Hmm, it would be very useful to do this but I am note sure its possible.  (on linux) SetEnv is a bash command, so not something you can use within a lazarus application. Except as you did with 'export', in both cases the setting will apply to the new process created, not the one in your running process. Does not seem to be anything in osutilsh which is where I would expect it to be if it existed. I guess you want to set this var AFTER the app has started and had a think about it ?  And I also guess you are are aware that the normal way to set a var for an app (in eg bash) is - MYSPECIALVAR="SomeValue" ./myApp Davo edit: MacOS ? Oh dear, probably need to put something in the app bundle like the above. ---------------------------------------------------------------------- trev · 2022-02-13 04:51 Here's a good explanation which I saved a few years ago: QuoteWhen a UNIX process is created by the kernel, the environment variables are put in its address space; typically near the top of the stack. They are not stored in the kernel data structures for the process. And thus there are no system calls for getting or setting those variables. ---------------------------------------------------------------------- trev · 2022-02-13 04:59 Quote from: Manlio on February 13, 2022, 04:29:33 amSo the question is, how do I do it on MacOS [sic]? So, can someone kindly show me how to do it? On macOS, the normal way to do it is to set any needed environment variables in the LSEnvironment property of your application's Info.plist file. See: https://developer.apple.com/documentation/bundleresources/informa tion_property_list/lsenvironment ---------------------------------------------------------------------- Manlio · 2022-02-13 14:02 Quote from: trev on February 13, 2022, 05:59:30 amOn macOS, the normal way to do it is to set any needed environment variables in the LSEnvironment property of your application's Info.plist file. See: https://developer.apple.com/documentation/bundleresources/informa tion_property_list/lsenvironment Yes, I can set environment variables via Info.plist, but I would still need to be able to do so at runtime, once I know things like the folder where the program is running from, and what other program may or may not be installed on the machine. Also let me raise a loud WARNING that might save someone a lot of time and headache: After I change the data in Info.plist and run the program again, NOTHING HAPPENS because macOS apparently caches the Info.plist data the first time you run the program! So you go on making changes, testing them, and get wrong results because you think the program is using the Info.plist file which is on the disk, but instead macOS is still using the previous cached version!!! I didn't find any way to prevent this, and only rebooting the program, or renaming/copying the whole folder that contains the program, forces macOS to load the new version of Info.plist. I wish I had known that before spending two hours to figure this out.... ---------------------------------------------------------------------- trev · 2022-02-14 00:04 Quote from: Manlio on February 13, 2022, 03:02:52 pmYes, I can set environment variables via Info.plist, but I would still need to be able to do so at runtime, once I know things like the folder where the program is running from, and what other program may or may not be installed on the machine. As explained above this is not possible from within the application - it's just the way UNIX is. ---------------------------------------------------------------------- Thaddy · 2022-02-14 09:20 Quote from: trev on February 14, 2022, 01:04:47 amQuote from: Manlio on February 13, 2022, 03:02:52 pmYes, I can set environment variables via Info.plist, but I would still need to be able to do so at runtime, once I know things like the folder where the program is running from, and what other program may or may not be installed on the machine. As explained above this is not possible from within the application - it's just the way UNIX is. Well, it should be: https://phoenixnap.com/kb/linux-set-environment- variable Will add example later if it works. Note that on Windows, if you add anything at runtime, the old environment is discarded and a new one is created. On linux one should edit the hidden file ~/.bashrc, but that needs sudo rights or a program that has sufficient rights. ---------------------------------------------------------------------- MarkMLl · 2022-02-14 10:38 Quote from: trev on February 14, 2022, 01:04:47 amAs explained above this is not possible from within the application - it's just the way UNIX is. Up to a point, Lord Copper. In principle a process can manipulate its own variables, which I think it what OP was asking. After all, that's how a shell sets its PATH variable. /However/, any assumption that a process will make use of the changed variable to e.g. locate a library is unsafe. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-02-14 11:27 Editing .bashrc seems to work, but I don't know if it works on Apples. (My Mac Mini died as you all know, or in coma) At least a Linux version under Debian works (Raspi and x86_64), provided admin rights. It only needs a TFileStream or better a TStringlist. ---------------------------------------------------------------------- PascalDragon · 2022-02-14 14:24 Quote from: Thaddy on February 14, 2022, 10:20:19 amOn linux one should edit the hidden file ~/.bashrc, but that needs sudo rights or a program that has sufficient rights. Editing one's own .bashrc does never require root rights, cause that file is supposed to be editable by the owning user (and ~ points to the home directory of the user that started the process). But editing the file will only work for shells that do indeed parse ~/.bashrc. For example a shell like zsh won't touch that file by default. ---------------------------------------------------------------------- winni · 2022-02-14 16:08 Hi! If you change the .bashrc you have to reread it. This is  done in the current session with . ~/.profile Danger! If you just delete a line this will not change the environment var. Winni ---------------------------------------------------------------------- Thaddy · 2022-02-14 16:57 @Sarah and @Winni, Yes I know, but it was the question if it could be done (changing/adding to the environment from running code). more or less a proof of concept. My idea is not ready to be in production, but then again: It can be done. Regards, tdk. ---------------------------------------------------------------------- MarkMLl · 2022-02-14 17:34 Quote from: winni on February 14, 2022, 05:08:00 pmIf you change the .bashrc you have to reread it. This is  done in the current session with . ~/.profile No it won't. In the general case those are distinct files used by different shells. MarkMLl ---------------------------------------------------------------------- Thaddy · 2022-02-14 17:52 Correct. ---------------------------------------------------------------------- MarkMLl · 2022-02-14 19:48 Quote from: PascalDragon on February 14, 2022, 03:24:16 pmEditing one's own .bashrc does never require root rights, cause that file is supposed to be editable by the owning user (and ~ points to the home directory of the user that started the process). I'm slightly uncomfortable with that. I can't put a finger on it, but I think I've seen legitimate situations where a user did not (and should not) have access to that file... and that's particularly the case where a daemon was started as e.g. root:root and then was relaxed to a nominal uid:gid like nobody:nogroup or daemon:root. In any event, OP specifically said QuoteI only want to add this environmental variable to the current running process. This is useful, for example, to pass information to a dylib.(1) so I don't think that .bashrc or .profile is really relevant. MarkMLl ---------------------------------------------------------------------- dbannon · 2022-02-14 21:42 > so I don't think that .bashrc or .profile is really relevant. Of course not. A new shell process is created, it reads .bashrc, builds an env  and then starts the user's app passing the built env. The only way that the content of .bashrc is relevant is if you then start a new shell and process.  So, the app finds it needs a different env, it alters .bashrc, starts a whole new process and exits itself. I guess thats OK as long as that particular var is not of any significance to any other process. But better to set the necessary env var, fork a new process to run the new version of the app and leave .bashrc alone. By the way, the only time .bashrc is inaccessible to the 'user' is if its a dummy user. Not a normal user login account. I have done things like that when a limited capability account is needed, guest usage etc. But there are far better and safer ways to do even that. Davo ---------------------------------------------------------------------- dbannon · 2022-02-15 03:37 Here is an example of the approach I mentioned above, while its probably not directly applicable, it demonstrates the basic idea and should be easily adaptable. Note, be careful you ensure the second process really does set something that will stop the loop, it could go on forever, or, perhaps until you run out of memory. All it does is startup, sees if it can find a env var called BLAR set, if not, it launches a new process with BLAR set as the only environment variable.  Your real app needs to only run in the process that does have BLAR, the first iteration just waits for the second one to finish. Davo ---------------------------------------------------------------------- Warfley · 2022-02-15 11:22 SetEnv (the C function) is part of the POSIX standard, and MacOS is a certified UNIX system and as such fully POSIX compliant. So SetEnv should be available. If FPC does not provide a wrapper for that function, you could link against the libc to call it ---------------------------------------------------------------------- dbannon · 2022-02-15 11:26 Interesting approach ! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i int setenv(const char *name, const char *value, int overwrite); I'm guessing we don't have binding, yet ? Davo ---------------------------------------------------------------------- Manlio · 2022-02-15 16:38 Quote from: Warfley on February 15, 2022, 12:22:08 pmSetEnv (the C function) is part of the POSIX standard, and MacOS is a certified UNIX system and as such fully POSIX compliant. So SetEnv should be available. If FPC does not provide a wrapper for that function, you could link against the libc to call it So you mean something like this: https://www.ibm.com/docs/en/zos/2.4.0?topic=functions-setenv-add- delete-change-environment-variables Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TSetEnv = function(const name: pchar; const value: pchar; flags: integer): integer; cdecl;var  SetEnv: TSetEnv;  Lib: Handle;begin  Lib := LoadLibrary('libc');  SetEnv := GetProcAddress(lib, 'setenv');  ...  SetEnv('MYNAME', 'myvalue', 1);  end;  Does that look right? I'm going to test it and see if it works, but if you see any error or change needed, please correct me, thanks! ---------------------------------------------------------------------- Warfley · 2022-02-15 16:58 You can directly link this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function SetEnv(const name: pchar; const value: pchar; flags: integer): integer; cdecl; external 'libc' name 'setenv';maybe it is not 'libc' but only 'c' or maybe even 'StdCLib'. You can look at the cmem.pp for examples on how to link against the libc