You are not logged in.

#1 2011-11-26 00:11:22

scrawl
Member
Registered: 2010-01-14
Posts: 44

trick applications to use different $HOME

My problem: I play games a lot and want to sync my savegames and settings between laptop and pc.

This is my approach: I have a launch script that every game gets run with. This launch script changes the $HOME environment variable to a special "Savegames" folder in my home directory, now I can just sync this whole folder.

This works fine with most games I've tried until now. However there is one (Prey) that still puts the savegames in $HOME/.prey instead of $HOME/Savegames/.prey !

So, essentially, my question is: What's the best way to trick a program that I have a different home directory?

Offline

#2 2011-11-26 00:58:39

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,802

Re: trick applications to use different $HOME

You might try creating ~/Savegames/.prey and then make a softlink :
ln -s ~/Savegames/.prey ~/.prey

Note that that won't work if ~/.prey already exists.  You may want to create ~/Savegames/.prey by moving ~/.prey the the Savegames directory


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2011-11-26 06:43:08

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: trick applications to use different $HOME

Probably by setting the XDG_CONFIG_HOME variable for your game specifically.


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#4 2011-11-26 12:55:42

scrawl
Member
Registered: 2010-01-14
Posts: 44

Re: trick applications to use different $HOME

ewaller wrote:

You might try creating ~/Savegames/.prey and then make a softlink :
ln -s ~/Savegames/.prey ~/.prey

I already considered that, the drawback is that I have to tell the script the save game folder for every game, which I don't really want to do...

.:B:. wrote:

Probably by setting the XDG_CONFIG_HOME variable for your game specifically.

I've now literally set every XDG_* and any other environment variable that had my login name in it. Still the game finds out about my real home folder.

Offline

#5 2011-11-26 17:07:17

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,802

Re: trick applications to use different $HOME

You can always edit the source wink


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#6 2011-11-26 19:04:09

scrawl
Member
Registered: 2010-01-14
Posts: 44

Re: trick applications to use different $HOME

Not for commercial games... wish I could sad

I guess I'll go for the symlink way then, even if its stupid...

Last edited by scrawl (2011-11-26 19:04:37)

Offline

#7 2011-11-27 02:31:37

SlayingDragons
Member
Registered: 2011-07-12
Posts: 42

Re: trick applications to use different $HOME

While the symlink way is probably the best way to do it, if you want to change your home directory in the bash session you're in, you can enter:

export HOME=/home/*user*/Savegames/.prey

Which if you're launching the game from a terminal should make it recognize it as your home folder.

Offline

#8 2011-11-27 23:32:49

putte_xvi
Member
From: Sweden
Registered: 2009-04-10
Posts: 22

Re: trick applications to use different $HOME

It could be reading the home directory using the getpwnam(3) function, or directly from /etc/passwd. You can probably find out by running something like:

ltrace -e getpwnam,getpwuid,getpwnam_r,getpwuid_r my_prey_executable >/dev/null

If this is the case, given that we don't have the source, maybe one could write a library with a fake version of the appropriate function and set LD_PRELOAD to make the game use it?

Or maybe it's simply writing stuff to the current directory and you just happen to always launch it from your home directory. smile

If all else fails, you could run the games as a separate user (using su or sudo) and treat that user's home folder as the savegames folder.

Offline

#9 2011-11-28 00:00:16

scrawl
Member
Registered: 2010-01-14
Posts: 44

Re: trick applications to use different $HOME

ltrace -e getpwnam,getpwuid,getpwnam_r,getpwuid_r ./prey.x86 >/dev/null
getpwuid(1000, 0xf73c22a0, 1, 0, 0x84fb520)      = 0xf7577c70
getpwuid(1000, 0, 248144, -1, -1)                = 0xf7577c70

If this is the case, given that we don't have the source, maybe one could write a library with a fake version of the appropriate function and set LD_PRELOAD to make the game use it?

Sounds like overkill big_smile

Last edited by scrawl (2011-11-28 00:00:26)

Offline

#10 2011-11-28 01:26:49

putte_xvi
Member
From: Sweden
Registered: 2009-04-10
Posts: 22

Re: trick applications to use different $HOME

scrawl wrote:
ltrace -e getpwnam,getpwuid,getpwnam_r,getpwuid_r ./prey.x86 >/dev/null
getpwuid(1000, 0xf73c22a0, 1, 0, 0x84fb520)      = 0xf7577c70
getpwuid(1000, 0, 248144, -1, -1)                = 0xf7577c70

If this is the case, given that we don't have the source, maybe one could write a library with a fake version of the appropriate function and set LD_PRELOAD to make the game use it?

Sounds like overkill big_smile

Well, I decided to give it a try. smile I'm not really a C programmer, but it was actually much easier than expected. This was quite helpful: http://www.technovelty.org/code/c/override.html

Save the code below as libfakehome.c and change the path (FAKE_HOMEDIR) to what you want:

#define FAKE_HOMEDIR "/opt/games"
#define FAKE_HOMEDIR_UID 1000

#define _GNU_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <dlfcn.h>

struct passwd *getpwuid(uid_t uid) {
    static struct passwd fake;
    struct passwd *(*real_getpwuid)(uid_t) = dlsym(RTLD_NEXT, "getpwuid");
    struct passwd *pwentry;
    pwentry = real_getpwuid(uid);
    if (uid != FAKE_HOMEDIR_UID) {
        return pwentry;
    }
    if (pwentry == NULL) {
        fprintf(stderr, "Failed to fake home directory.\n");
        return NULL;
    }
    fake = *pwentry;
    fake.pw_dir = FAKE_HOMEDIR;
    fprintf(stderr, "Faking home directory: %s\n", fake.pw_dir);
    return &fake;
}

Compile it:

gcc -shared -fPIC -ldl -o /somewhere/libfakehome.so libfakehome.c

And run it:

LD_PRELOAD=/somewhere/libfakehome.so ./prey.x86

Offline

#11 2011-11-28 16:02:16

scrawl
Member
Registered: 2010-01-14
Posts: 44

Re: trick applications to use different $HOME

Wow, that looks really cool!

How would I compile it for x86 (Since I'm running 64 bit arch, but prey is 32 bit)?

Edit: Solved: install gcc-multilib and compile with -m32 flag.

and it just works smile Thank you very much


Maybe I will release something when I'm finished. I'm doing a collection of scripts to make installing/running games across multiple linuxes easier (Current features are shared savegame folder, and script to launch on different XServer for games that don't allow you to tab out - in the future maybe also wine support and a game database ala playonlinux)

Last edited by scrawl (2011-11-28 16:09:51)

Offline

Board footer

Powered by FluxBB