Skip to content

Commit

Permalink
Allow running stack as non-root user #21
Browse files Browse the repository at this point in the history
Use own config file: /etc/remotepi.conf or ~/.remotepi.conf.
Take rootdir for file browsing from rootdir= option.

When started as non-root, run in ~/.remotepi folder.
Start own omxd daemon instance running as same user.
  • Loading branch information
subogero committed Apr 18, 2017
1 parent a31079b commit b8f4bbc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
23 changes: 19 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ As root
This installs RemotePi's dependencies too: omxd, rpi.fm, u2b
and the necessary Perl libraries.

## RUNNING REMOTEPI AS NON_ROOT USER

Mounting your media as a FUSE filesystem as a non-root user may necessitate
running the remotepi stack as the same user.

This is entirely possible, by e.g. a simple entry in the user's crontab

@reboot /usr/share/remotepi/remotepi 3000 >~/.remotepi.log 2>&1 &

In this case remotepi will listen on port 3000 and run it's own omxd instance
as the same user, with all control and log files in folder ~/.remotepi.

Location of the config file will change to ~/.remotepi.conf. See FILES.

## USE

Point your PC/tablet/phone's browser to http://raspberry:31100 on your
Expand Down Expand Up @@ -62,12 +76,13 @@ RemotePi offers a JSON-based REST API to control all functions:

## FILES

### /etc/omxd.conf
Location of files depends on whether remotepi runs as root.

### /etc/remotepi.conf ~/.remotepi.conf

Omxd runs omxplayer as the user specified in this file.
This user's home is the root directory for file browsing by RemotePi.
Contains the rootdir options for remotepi's pi file-browsing tab.
If the file does not exist, the default root directory is /home.

### /usr/share/remotepi/.rpi.fm
### /usr/share/remotepi/.rpi.fm ~/.remotepi/.rpi.fm

rpi.fm's genre cache and "My stations" database
34 changes: 22 additions & 12 deletions remotepi
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,28 @@ our ($img_dir, $img);
# Clean up albumart symlink upon exit
$SIG{TERM} = sub { thumbnail };

# Get ls root directory
if (open CFG, "/etc/omxd.conf") {
$root = <CFG>;
chomp $root;
$root =~ s|user=|/home/|;
$root = "/home" unless -d $root;
close CFG;
} else {
$root = "/home";
}
# Check user
my @userdata = getpwuid $< or die "Unkown user id $<";
my ($user, $home) = @userdata[0, -2];

# Run in /var/tmp/remotepi
my $cwd = '/var/tmp/remotepi';
# Run in /var/tmp/remotepi or ~/.remotepi
my $cwd = $user eq 'root' ? '/var/tmp/remotepi' : "$home/.remotepi";
-d $cwd or mkdir $cwd;
chdir $cwd;

# Start own omxd instance unless running as root
system "omxd" if $user ne 'root';

# Get ls root directory
my $cfgfile = $user eq 'root' ? '/etc/remotepi.conf' : "$home/.remotepi.conf";
$root = '/home';
if (open my $cfg, $cfgfile) {
while (<$cfg>) {
chomp;
$root = $1 if /rootdir=(.*)/ && -d $1;
}
}

# ROUTES
get '/' => sub {
my $c = shift;
Expand Down Expand Up @@ -102,6 +108,7 @@ websocket '/diff' => sub {

rpifm_my;

# START APP
push @{ app->static->paths }, $cwd;
my $port = shift // 31100;
app->start('daemon', '-l' => "http://*:$port");
Expand Down Expand Up @@ -291,6 +298,9 @@ sub fm {

sub run_rpifm {
my $cmd = shift;
# Force rpi.fm to store status in current dir
local %ENV;
delete $ENV{HOME};
my $pid = open2(\*IN, \*OUT, '/usr/bin/rpi.fm') or die $!;
print OUT $cmd;
close OUT;
Expand Down

0 comments on commit b8f4bbc

Please sign in to comment.