Skip to content
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
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ odkrun_SOURCES = src/odkrun.c \
src/backend-docker.c src/backend-docker.h \
src/backend-singularity.c src/backend-singularity.h \
src/backend-native.c src/backend-native.h \
src/backend-apple.c src/backend-apple.h \
src/owlapi.c src/owlapi.h src/owlapi-options.h \
src/runconf.c src/runconf.h \
src/oaklib.h src/oaklib.c \
Expand Down
5 changes: 5 additions & 0 deletions doc/odkrun.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ odkrun - ODK Runner tool
.IR tag ]
.RB [ -l | --lite ]
.RB [ -s | --singulary ]
.RB [ --apple ]
.RB [ -n | --native ]
.RB [ --root ]
.RB [ -e | --env
Expand Down Expand Up @@ -64,6 +65,10 @@ Use the \fIobolibrary/odklite\fR image.
.BR -s ", " --singularity
Run the container with Singularity rather than Docker. This
is experimental but should mostly work.
.BR --apple
Run the container with Apple Container rather than Docker.
This is only available on macOS. This is experimental but
should mostly work.
.TP
.BR -n ", " --native
Run in the native system rather than in a container. This is
Expand Down
137 changes: 137 additions & 0 deletions src/backend-apple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* ODK Runner
* Copyright (C) 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "backend-apple.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "procutil.h"
#include "util.h"
#include "backend-docker.h"

#if defined(ODK_RUNNER_MACOS)

static int
prepare(odk_backend_t *backend, odk_run_config_t *cfg)
{
(void) backend;

if ( (cfg->flags & ODK_FLAG_RUNASROOT) == 0 ) {
odk_add_env_var(cfg, "ODK_USER_ID", "1000", 0);
odk_add_env_var(cfg, "ODK_GROUP_ID", "1000", 0);
}

return 0;
}

static int
run(odk_backend_t *backend, odk_run_config_t *cfg, char **command)
{
int rc;
char **argv;
mem_registry_t mr = { 0 };

(void) backend;

argv = odk_backend_docker_build_command(&mr, cfg, command);
argv[0] = "container";
rc = spawn_process(argv);
mr_free(&mr);

return rc;
}

static int
close_backend(odk_backend_t *backend)
{
(void) backend;

return 0;
}

static int
get_total_memory(odk_backend_info_t *info)
{
FILE *p;
int ret = -1;

if ( (p = popen("container system property ls", "r")) != NULL ) {
char line[128];
ssize_t n;
int memory, container_section = 0;

/*
* Contrary to `docker info`, `container system property ls`
* does not allow to query an individual setting, so we need to
* parse the entire output.
*/
while ( ! feof(p) ) {
if ( (n = get_line(p, line, sizeof(line))) > 0 ) {
if ( line[0] == '[' ) {
container_section = strcmp(line, "[container]") == 0;
}
else if ( container_section && sscanf(line, "memory = \"%dgb\"", &memory) == 1 )
info->total_memory = (long) memory * 1024 * 1024 * 1024;
}
}
pclose(p);
ret = 0;
}

return ret;
}

#endif /* ODK_RUNNER_MACOS */

int
odk_backend_apple_init(odk_backend_t *backend)
{
#if !defined(ODK_RUNNER_MACOS)
errno = ENOSYS;
return -1;

#else
int ret;

backend->prepare = prepare;
backend->run = run;
backend->close = close_backend;

ret = get_total_memory(&(backend->info));

return ret;
#endif
}
47 changes: 47 additions & 0 deletions src/backend-apple.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* ODK Runner
* Copyright (C) 2026 Damien Goutte-Gattat
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef ICP20260928_BACKEND_APPLE_H
#define ICP20260928_BACKEND_APPLE_H

#include "backend.h"

#ifdef __cpluscplus
extern "C" {
#endif

int
odk_backend_apple_init(odk_backend_t *);

#ifdef __cpluscplus
}
#endif

#endif /* !ICP20260928_BACKEND_APPLE_H */
82 changes: 44 additions & 38 deletions src/backend-docker.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
#include <unistd.h> /* for getuid/getgid */
#endif

#include <memreg.h>

#include "procutil.h"
#include "util.h"

Expand Down Expand Up @@ -87,12 +85,49 @@ static int
run(odk_backend_t *backend, odk_run_config_t *cfg, char **command)
{
int rc;
size_t n, i = 0;
char **argv, **cursor, *image_qualifier;
char **argv;
mem_registry_t mr = { 0 };

(void) backend;

argv = odk_backend_docker_build_command(&mr, cfg, command);
rc = spawn_process(argv);
mr_free(&mr);

return rc;
}

static int
close_backend(odk_backend_t *backend)
{
(void) backend;

return 0;
}

static int
get_total_memory(odk_backend_info_t *info)
{
FILE *p;
int ret = -1;

if ( (p = popen("docker info --format={{.MemTotal}}", "r")) != NULL ) {
if ( fscanf(p, "%lu", &(info->total_memory)) == 1 )
ret = 0;
else
errno = ESRCH;
pclose(p);
}

return ret;
}

char **
odk_backend_docker_build_command(mem_registry_t *mr, odk_run_config_t *cfg, char **command)
{
size_t n, i = 0;
char **argv, **cursor, *image_qualifier;

image_qualifier = strchr(cfg->image_name, '/') ? "" : "obolibrary/";

/* Number of tokens in the command line */
Expand All @@ -105,7 +140,7 @@ run(odk_backend_t *backend, odk_run_config_t *cfg, char **command)
n += 1;

/* Assembling the command line */
argv = mr_alloc(&mr, sizeof(char *) * n);
argv = mr_alloc(mr, sizeof(char *) * n);
argv[i++] = "docker";
argv[i++] = "run";
argv[i++] = "--rm";
Expand All @@ -114,17 +149,17 @@ run(odk_backend_t *backend, odk_run_config_t *cfg, char **command)
argv[i++] = (char *)cfg->work_directory;
for ( int j = 0; j < cfg->n_bindings; j++ ) {
argv[i++] = "-v";
argv[i++] = mr_sprintf(&mr, "%s:%s", cfg->bindings[j].host_directory, cfg->bindings[j].container_directory);
argv[i++] = mr_sprintf(mr, "%s:%s", cfg->bindings[j].host_directory, cfg->bindings[j].container_directory);
}
for ( int j = 0; j < cfg->n_env_vars; j++ ) {
if ( cfg->env_vars[j].value != NULL ) {
argv[i++] = "-e";
argv[i++] = mr_sprintf(&mr, "%s=%s", cfg->env_vars[j].name, cfg->env_vars[j].value);
argv[i++] = mr_sprintf(mr, "%s=%s", cfg->env_vars[j].name, cfg->env_vars[j].value);
}
}
for ( int j = 0; j < cfg->n_backend_opts; j++ )
argv[i++] = (char *)cfg->backend_opts[j].name;
argv[i++] = mr_sprintf(&mr, "%s%s:%s", image_qualifier, cfg->image_name, cfg->image_tag);
argv[i++] = mr_sprintf(mr, "%s%s:%s", image_qualifier, cfg->image_name, cfg->image_tag);
if ( cfg->flags & ODK_FLAG_TIMEDEBUG ) {
argv[i++] = "/usr/bin/time";
argv[i++] = "-f";
Expand All @@ -138,36 +173,7 @@ run(odk_backend_t *backend, odk_run_config_t *cfg, char **command)
argv[i++] = *cursor;
argv[i] = NULL;

/* Execute */
rc = spawn_process(argv);
mr_free(&mr);

return rc;
}

static int
close_backend(odk_backend_t *backend)
{
(void) backend;

return 0;
}

static int
get_total_memory(odk_backend_info_t *info)
{
FILE *p;
int ret = -1;

if ( (p = popen("docker info --format={{.MemTotal}}", "r")) != NULL ) {
if ( fscanf(p, "%lu", &(info->total_memory)) == 1 )
ret = 0;
else
errno = ESRCH;
pclose(p);
}

return ret;
return argv;
}

int
Expand Down
5 changes: 5 additions & 0 deletions src/backend-docker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@
#ifndef ICP20240622_BACKEND_DOCKER_H
#define ICP20240622_BACKEND_DOCKER_H

#include <memreg.h>

#include "backend.h"

#ifdef __cpluscplus
extern "C" {
#endif

char **
odk_backend_docker_build_command(mem_registry_t *, odk_run_config_t *, char **);

int
odk_backend_docker_init(odk_backend_t *);

Expand Down
Loading
Loading