Skip to content

Set required environmental variables for rootless docker-compose ACAP #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions app/dockerdwrapperwithcompose.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
#include <netinet/in.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>

#define APP_NAME "dockerdwrapperwithcompose"

/** @brief APP path in a device */
#define APP_DIRECTORY "/usr/local/packages/" APP_NAME

/**
* @brief Callback called when the dockerd process exits.
*/
Expand Down Expand Up @@ -668,6 +674,46 @@ main(void)
openlog(NULL, LOG_PID, LOG_USER);
syslog(LOG_INFO, "Started logging.");

// Get UID of the current user
uid_t uid = getuid();

char path[strlen(APP_DIRECTORY) + 256];
sprintf(path,
"/bin:/usr/bin:%s:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin",
APP_DIRECTORY);

char docker_host[256];
sprintf(docker_host, "unix://run/user/%d/docker.sock", (int)uid);

char xdg_runtime_dir[256];
sprintf(xdg_runtime_dir, "/run/user/%d", (int)uid);

// Set environment variables
if (setenv("PATH", path, 1) != 0) {
syslog(LOG_ERR, "Error setting environment PATH.");
return -1;
}

if (setenv("HOME", APP_DIRECTORY, 1) != 0) {
syslog(LOG_ERR, "Error setting environment APP_LOCATION.");
return -1;
}

if (setenv("DOCKER_HOST", docker_host, 1) != 0) {
syslog(LOG_ERR, "Error setting environment DOCKER_HOST.");
return -1;
}

if (setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1) != 0) {
syslog(LOG_ERR, "Error setting environment XDG_RUNTIME_DIR.");
return -1;
}

syslog(LOG_INFO, "PATH: %s", path);
syslog(LOG_INFO, "HOME: %s", APP_DIRECTORY);
syslog(LOG_INFO, "DOCKER_HOST: %s", docker_host);
syslog(LOG_INFO, "XDG_RUNTIME_DIR: %s", xdg_runtime_dir);

// Setup signal handling.
init_signals();

Expand Down
4 changes: 0 additions & 4 deletions app/postinstallscript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ Wants=network-online.target
After=user@$_uid.service
Requires=user@$_uid.service
[Service]
Environment=PATH=/bin:/usr/bin:$_appdirectory:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=HOME=$_appdirectory
Environment=DOCKER_HOST=unix://run/user/$_uid/docker.sock
Environment=XDG_RUNTIME_DIR=/run/user/$_uid
ExecStartPre=+$_appdirectory/handle_directories.sh $_uid $_uname $_gname
EOF

Expand Down