I'm trying to indirectly run VLC media player on Linux from a program that runs as root (i.e. sudo ./capture
). After running into some error codes that VLC doesn't run as root, I attempted to de-escalate the program's privileges before running, as such
setuid(1000);
setgid(1000);
putenv("HOME=/home/musicman");
putenv("LOGNAME=musicman");
putenv("USER=musicman");
putenv("DISPLAY=:0");
putenv("DESKTOP_SESSION=plasma");
printf("Running args: ");
char* ARGS2[] = {"cvlc", "file:///home/musicman/bad_apple.mp3", "vlc://quit", NULL};
for (int i = 0; i < 3; i++)
{
printf("'%s' ", ARGS2[i]);
}
printf("\n");
execvp(ARGS2[0], ARGS2);
Now, while the intermediary python program runs fine, VLC itself generates the following errors:
Running args: 'cvlc' 'file:///home/musicman/bad_apple.mp3' 'vlc://quit'
VLC media player 3.0.12 Vetinari (revision 3.0.12-1-0-gd147bb5e7e)
[0000557de32b32c0] vlcpulse audio output error: PulseAudio server connection failure: Connection refused
[0000557de3354f70] dummy interface: using the dummy interface module...
ALSA lib pcm_dmix.c:1075:(snd_pcm_dmix_open) unable to open slave
[0000557de32b32c0] alsa audio output error: cannot open ALSA device "default": Device or resource busy
[0000557de32b32c0] main audio output error: Audio output failed
[0000557de32b32c0] main audio output error: The audio device "default" could not be used:
Device or resource busy.
[0000557de32b32c0] main audio output error: module not functional
[00007f5094c48220] main decoder error: failed to create audio output
However, if I just run the following code without sudo
char* ARGS2[] = {"cvlc", "file:///home/musicman/bad_apple.mp3", "vlc://quit", NULL};
printf("Running args: ");
for (int i = 0; i < 3; i++)
{
printf("'%s' ", ARGS2[i]);
}
printf("\n");
execvp(ARGS2[0], ARGS2);
I get the following output:
Running args: 'cvlc' 'file:///home/musicman/bad_apple.mp3' 'vlc://quit'
VLC media player 3.0.12 Vetinari (revision 3.0.12-1-0-gd147bb5e7e)
[00005570414ddd00] dummy interface: using the dummy interface module...
...and the song plays.
I suspect that, for some reason, PulseAudio is relying on some environmental variables that I'm not providing, but I'm really not sure what I'm missing, as running env | egrep "^PULSE"
reveals nothing. Do any of you have ideas?
I'm running on Kubuntu 21.04 with an X11 session and pulseaudio 14.2.