Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
onnx_runtime.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file onnx_runtime.c
19 * @brief Local-only dynamic loading and process-lifetime ONNX environment ownership.
20 * @details Keeping one retained API avoids incompatible per-module loaders and dangling session callbacks. */
21#include "onnx_runtime.h"
22
23#include <onnxruntime_c_api.h>
24#include <dlfcn.h>
25#include <errno.h>
26#include <limits.h>
27#include <pthread.h>
28#include <stdio.h>
29#include <string.h>
30#include <sys/stat.h>
31#include <unistd.h>
32
33static pthread_mutex_t runtime_mutex = PTHREAD_MUTEX_INITIALIZER;
34static const OrtApi *runtime_api;
35static OrtEnv *runtime_environment;
36static void *runtime_library;
37
38bool onnx_runtime_open(const char *library_directory, char *error, size_t capacity)
39{
40 if (!error || !capacity) return false;
41 error[0] = '\0';
42 const int locked = pthread_mutex_lock(&runtime_mutex);
43 if (locked) {
44 snprintf(error, capacity, "Cannot lock ONNX runtime: %s", strerror(locked));
45 return false;
46 }
47 if (runtime_api) {
48 pthread_mutex_unlock(&runtime_mutex);
49 return true;
50 }
51 char executable[PATH_MAX];
52 const ssize_t length = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
53 if (length <= 0 || (size_t)length >= sizeof(executable) - 1) {
54 snprintf(error, capacity, "Cannot resolve executable for ONNX runtime");
55 pthread_mutex_unlock(&runtime_mutex);
56 return false;
57 }
58 executable[length] = '\0';
59 char *separator = strrchr(executable, '/');
60 if (!separator) {
61 snprintf(error, capacity, "Invalid executable path for ONNX runtime");
62 pthread_mutex_unlock(&runtime_mutex);
63 return false;
64 }
65 *separator = '\0';
66 char parent[PATH_MAX];
67 snprintf(parent, sizeof(parent), "%s", executable);
68 for (unsigned level = 0; level < 2; ++level) {
69 char *slash = strrchr(parent, '/');
70 if (!slash || slash == parent) {
71 parent[0] = '/';
72 parent[1] = '\0';
73 break;
74 }
75 *slash = '\0';
76 }
77 char secondary[PATH_MAX];
78 const int secondary_length = snprintf(secondary, sizeof(secondary), "%s%sbin", parent,
79 parent[strlen(parent) - 1] == '/' ? "" : "/");
80 if (secondary_length < 0 || (size_t)secondary_length >= sizeof(secondary)) {
81 snprintf(error, capacity, "ONNX runtime directory exceeds path capacity");
82 pthread_mutex_unlock(&runtime_mutex);
83 return false;
84 }
85 const char *directories[] = {executable, secondary, library_directory};
86 const char *names[] = {
87#if defined(__aarch64__)
88 /* Architecture suffixes let ARM64 and host libraries coexist in the development bin directory. */
89 "libonnxruntime.so-arm64", "libonnxruntime.so.1-arm64", "libonnxruntime.so.1.31.0-arm64",
90#endif
91 "libonnxruntime.so", "libonnxruntime.so.1", "libonnxruntime.so.1.31.0"
92 };
93 bool opened = false;
94 for (size_t directory = 0; directory < sizeof(directories) / sizeof(directories[0]); ++directory) {
95 if (!directories[directory] || !directories[directory][0]) continue;
96 for (size_t name = 0; name < sizeof(names) / sizeof(names[0]); ++name) {
97 char filename[PATH_MAX];
98 const int count = snprintf(filename, sizeof(filename), "%s/%s", directories[directory], names[name]);
99 if (count < 0 || (size_t)count >= sizeof(filename)) {
100 snprintf(error, capacity, "ONNX runtime filename exceeds path capacity");
101 goto finished;
102 }
103 struct stat info;
104 if (stat(filename, &info) != 0) {
105 if (errno == ENOENT || errno == ENOTDIR) continue;
106 snprintf(error, capacity, "Cannot inspect ONNX Runtime %s: %s", filename, strerror(errno));
107 goto finished;
108 }
109 if (!S_ISREG(info.st_mode)) {
110 snprintf(error, capacity, "ONNX Runtime is not a regular file: %s", filename);
111 goto finished;
112 }
113 void *library = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
114 /* An existing broken candidate is a package error, not permission to try a different runtime. */
115 if (!library) {
116 snprintf(error, capacity, "Cannot load ONNX Runtime %s: %s", filename, dlerror());
117 goto finished;
118 }
119 union {
120 void *object;
121 const OrtApiBase *(*function)(void);
122 } symbol = {.object = dlsym(library, "OrtGetApiBase")};
123 /* Resolve through the C API so C and C++ consumers share the same ABI check. */
124 const OrtApiBase *base = symbol.function ? symbol.function() : NULL;
125 const OrtApi *api = base ? base->GetApi(ORT_API_VERSION) : NULL;
126 if (!api) {
127 snprintf(error, capacity, "Incompatible ONNX Runtime API in %s", filename);
128 dlclose(library);
129 goto finished;
130 }
131 OrtEnv *environment = NULL;
132 OrtStatus *status = api->CreateEnv(ORT_LOGGING_LEVEL_WARNING, "sensoraia", &environment);
133 if (status) {
134 snprintf(error, capacity, "Cannot initialize ONNX Runtime: %s", api->GetErrorMessage(status));
135 api->ReleaseStatus(status);
136 dlclose(library);
137 goto finished;
138 }
139 /* Retain this handle: environments and tensor objects may call the API until process exit. */
140 runtime_library = library;
141 runtime_environment = environment;
142 runtime_api = api;
143 opened = true;
144 goto finished;
145 }
146 }
147 snprintf(error, capacity,
148 "ONNX Runtime not found in searched directories: %s, %s, %s. "
149 "Tried libonnxruntime.so, libonnxruntime.so.1, libonnxruntime.so.1.31.0%s. "
150 "Copy a compatible runtime beside the executable or set onnx_lib_path in diplacam_settings.ini.",
151 executable, secondary, library_directory ? library_directory : "(not configured)",
152#if defined(__aarch64__)
153 " and their -arm64 variants"
154#else
155 ""
156#endif
157 );
158finished:
159 if (!opened) {
160 const size_t used = strlen(error);
161 if (used < capacity)
162 snprintf(error + used, capacity - used, ". Copy a compatible %s into %s or set onnx_lib_path in diplacam_settings.ini.",
163 names[0], executable);
164 }
165 pthread_mutex_unlock(&runtime_mutex);
166 return opened;
167}
168
169const OrtApi *onnx_runtime_api(void)
170{
171 pthread_mutex_lock(&runtime_mutex);
172 const OrtApi *api = runtime_library ? runtime_api : NULL;
173 pthread_mutex_unlock(&runtime_mutex);
174 return api;
175}
176
178{
179 pthread_mutex_lock(&runtime_mutex);
180 OrtEnv *environment = runtime_library ? runtime_environment : NULL;
181 pthread_mutex_unlock(&runtime_mutex);
182 return environment;
183}
static unsigned capacity[COVERAGE_CAMERAS]
struct jpeg_error_mgr error
Definition lwir_image.c:60
static const OrtApi * runtime_api
bool onnx_runtime_open(const char *library_directory, char *error, size_t capacity)
Initialize one process-lifetime runtime/environment using explicit local search roots.
static OrtEnv * runtime_environment
static pthread_mutex_t runtime_mutex
OrtEnv * onnx_runtime_environment(void)
Borrow the environment retained until process exit.
static void * runtime_library
const OrtApi * onnx_runtime_api(void)
Borrow the initialized API, without transferring release ownership.
Process-wide runtime ownership shared by independent model sessions.
#define PATH_MAX
Definition path_utils.h:39
static size_t count
Definition pose_log.c:79