Make socket directory location configurable via config.def.h

Introduce $ABDUCO_SOCKET_DIR environment variable to set a
default location.
This commit is contained in:
Marc André Tanner 2016-01-06 14:37:56 +01:00
parent 21dd6ad6b7
commit 3e9a3fdf4d
3 changed files with 36 additions and 25 deletions

View File

@ -56,13 +56,17 @@ is examined, if it is not set
.BR dvtm(1) .BR dvtm(1)
is executed. is executed.
By default all session related information is stored in All session related information is stored in the following directories (first
to succeed is used):
.RS
.nf
.PP
.B $ABDUCO_SOCKET_DIR/abduco
.B $HOME/.abduco .B $HOME/.abduco
with .B $TMPDIR/abduco/$USER
.BR $TMPDIR/abduco/$USER .B /tmp/abduco/$USER
as a fallback and .fi
.BR /tmp/abduco/$USER .RE
as a last resort.
However if a given session name represents either a relative or absolute path However if a given session name represents either a relative or absolute path
it is used unmodified. it is used unmodified.

View File

@ -276,29 +276,23 @@ static bool create_socket_dir(struct sockaddr_un *sockaddr) {
size_t maxlen = sizeof(sockaddr->sun_path); size_t maxlen = sizeof(sockaddr->sun_path);
uid_t uid = getuid(); uid_t uid = getuid();
struct passwd *pw = getpwuid(uid); struct passwd *pw = getpwuid(uid);
char *home = getenv("HOME");
if ((!home || !home[0]) && pw)
home = pw->pw_dir;
struct { for (unsigned int i = 0; i < countof(socket_dirs); i++) {
char *dir;
bool personal; /* whether it is a per user directory */
} dirs[] = {
{ home, true },
{ getenv("TMPDIR"), false },
{ "/tmp", false },
};
for (unsigned int i = 0; i < countof(dirs); i++) {
struct stat sb; struct stat sb;
char *dir = dirs[i].dir; struct Dir *dir = &socket_dirs[i];
bool ispersonal = dirs[i].personal; bool ishome = false;
if (!dir) if (dir->env) {
dir->path = getenv(dir->env);
ishome = !strcmp(dir->env, "HOME");
if (ishome && (!dir->path || !dir->path[0]) && pw)
dir->path = pw->pw_dir;
}
if (!dir->path || !dir->path[0])
continue; continue;
if (!xsnprintf(sockaddr->sun_path, maxlen, "%s/%s%s/", dir, dir == home ? "." : "", server.name)) if (!xsnprintf(sockaddr->sun_path, maxlen, "%s/%s%s/", dir->path, ishome ? "." : "", server.name))
continue; continue;
mode_t mask = umask(0); mode_t mask = umask(0);
int r = mkdir(sockaddr->sun_path, ispersonal ? S_IRWXU : S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX); int r = mkdir(sockaddr->sun_path, dir->personal ? S_IRWXU : S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
umask(mask); umask(mask);
if (r != 0 && errno != EEXIST) if (r != 0 && errno != EEXIST)
continue; continue;
@ -310,7 +304,7 @@ static bool create_socket_dir(struct sockaddr_un *sockaddr) {
} }
size_t dirlen = strlen(sockaddr->sun_path); size_t dirlen = strlen(sockaddr->sun_path);
if (!ispersonal) { if (!dir->personal) {
/* create subdirectory only accessible to user */ /* create subdirectory only accessible to user */
if (pw && !xsnprintf(sockaddr->sun_path+dirlen, maxlen-dirlen, "%s/", pw->pw_name)) if (pw && !xsnprintf(sockaddr->sun_path+dirlen, maxlen-dirlen, "%s/", pw->pw_name))
continue; continue;

View File

@ -1,2 +1,15 @@
static char KEY_DETACH = CTRL('\\'); static char KEY_DETACH = CTRL('\\');
static char KEY_REDRAW = 0; static char KEY_REDRAW = 0;
/* Where to place the "abduco" directory storing all session socket files.
* The first directory to succeed is used. */
static struct Dir {
char *path; /* fixed (absolute) path to a directory */
char *env; /* environment variable to use if (set) */
bool personal; /* if false a user owned sub directory will be created */
} socket_dirs[] = {
{ .env = "ABDUCO_SOCKET_DIR", false },
{ .env = "HOME", true },
{ .env = "TMPDIR", false },
{ .path = "/tmp", false },
};