You are not logged in.

#1 2010-02-10 12:23:49

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

changing env vars "upstream"? (how do you change MPD_HOST?)

on desktops, you usually have a process tree that goes something like (well, this is only one branch):

init───login───bash───startx───xinit─┬─.xinitrc───sessionmanager─┬─WM

we all know that env vars are passed on "down the tree". but this sometimes bothers me, for example when using mpd. it uses a variable MPD_HOST
so when i have a running desktop, and i want to use an other mpd instance I must change MPD_HOST.  I can only do this in the "local" environment.  the widgets in my WM, the interfaces that I use etc cannot be aware of the changed variable.

how do you guys handle this?
I have some ideas for solutions:
1) make MPD_HOST=mpdhost and use /etc/hosts to make it point to other things. but that won't help for MPD_PORT or non-mpd related variables.  not mentioning some apps might cache dns internally. and this requires root sad
2) hack my session manager so that i can somehow notify it (signals, let it listen to a socket, etc) and have it change an environment variable.  but i don't know if the changed variable will automatically "propagate downstream" (probably not).

any more ideas?
thanks.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#2 2010-02-10 23:30:51

sand_man
Member
From: Australia
Registered: 2008-06-10
Posts: 2,164

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

I recently asked about a similar issue setting the http_proxy variable when starting vpnc. Seems like there is no way to make the variable global and visible to all other processes other than the child processes.
I'd be interested in other people's comments.


neutral

Offline

#3 2010-02-11 13:11:06

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

I tend to use a wrapper script for the app i want to run and a file from which it sources the environment variable

ie in ~/.bin/mpc

#!/bin/bash
source ~/.env/mpc
/usr/bin/mpc "$@"

and the contents of ~/.env/mpc would look like that:

export MPD_HOST=localhost

Last edited by raf_kig (2010-02-11 13:12:48)

Offline

#4 2010-02-11 13:13:43

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

raf_kig wrote:

I tend to use a wrapper script for the app i want to run and a file from which it sources the environment variable
(..)

Explain how this should solve the problem?  I think you don't understand my question.

Last edited by Dieter@be (2010-02-11 13:14:14)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#5 2010-02-12 14:54:09

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

hm yeah, it obviously won't work for running instances of programs - it just makes sure that changes in env (in the respective env-file) take effect for all new instances of $program.

You can eg use gdb to change the env of a running process (have a look at this script for reference http://www.nanonanonano.net/linux/chenv) , but most of the time that won't help you. Typically env vars are only evaluated at startup and then used to populate the appropriate structures. You'll probably have to implement the required functionality yourself in every single program.

For MPD a more or less simple workaround might be writing a small proxy that allows you to switch its destination on the fly - but i don't know whether MPD's protocol is stateful and whether clients will handle the transition gracefully.

Last edited by raf_kig (2010-02-12 14:55:02)

Offline

#6 2010-02-12 18:08:28

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

I think what you want is not possible. You would have to use something like what raf_kig has and restart the program. fork copies the env vars; there is no shared address space.


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

#7 2010-02-12 18:17:59

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

I just found that in my bookmarks, you might want to take a look at it :-)

http://joey.kitenet.net/blog/entry/mpd_proxy/

Offline

#8 2010-02-13 09:11:47

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: changing env vars "upstream"? (how do you change MPD_HOST?)

aha mpd_proxy looks neat.  that joey dude is a cool guy, every once in a while I'm pointed to an article or program and it turns out to be on his site.

and too bad about the limitations with env vars sad but glad it's cleared up.
An alternative I'm thinking about: my WM (awesome) can reload in place without loosing state (though i'm not sure it can get a new environment while doing that), and i could script it so that it restarts ncmpcpp (my mpd client) when it restarts.


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

Board footer

Powered by FluxBB