File: plugin-host-sdk.c

package info (click to toggle)
foundry 1.1~beta-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,572 kB
  • sloc: ansic: 167,487; xml: 417; sh: 19; makefile: 13; javascript: 10
file content (284 lines) | stat: -rw-r--r-- 8,896 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* plugin-host-sdk.c
 *
 * Copyright 2024-2025 Christian Hergert <chergert@redhat.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include "foundry-build-pipeline.h"
#include "foundry-search-path.h"
#include "foundry-shell.h"
#include "foundry-util-private.h"

#include "plugin-host-sdk.h"

struct _PluginHostSdk
{
  FoundrySdk  parent_instance;
  char       *systemd_run_path;
  guint       in_flatpak : 1;
};

typedef struct _HostSdkPrepare
{
  PluginHostSdk        *self;
  FoundryBuildPipeline *pipeline;
} HostSdkPrepare;

G_DEFINE_FINAL_TYPE (PluginHostSdk, plugin_host_sdk, FOUNDRY_TYPE_SDK)

static void
host_sdk_prepare_free (HostSdkPrepare *prepare)
{
  g_clear_object (&prepare->self);
  g_clear_object (&prepare->pipeline);
  g_free (prepare);
}

static gboolean
plugin_host_sdk_systemd_run_handler (FoundryProcessLauncher  *launcher,
                                     const char * const      *argv,
                                     const char * const      *env,
                                     const char              *cwd,
                                     FoundryUnixFDMap        *unix_fd_map,
                                     gpointer                 user_data,
                                     GError                 **error)
{
  HostSdkPrepare *prepare = user_data;
  g_autofree char *uuid = NULL;
  g_autofree char *new_path = NULL;
  g_autofree char *pipeline_prepend = NULL;
  g_autofree char *pipeline_append = NULL;
  const char *path = NULL;

  g_assert (FOUNDRY_IS_PROCESS_LAUNCHER (launcher));
  g_assert (prepare != NULL);
  g_assert (PLUGIN_IS_HOST_SDK (prepare->self));

  if (!foundry_process_launcher_merge_unix_fd_map (launcher, unix_fd_map, error))
    return FALSE;

  foundry_process_launcher_set_cwd (launcher, cwd);

  foundry_process_launcher_append_argv (launcher, prepare->self->systemd_run_path);
  foundry_process_launcher_append_argv (launcher, "--user");
  foundry_process_launcher_append_argv (launcher, "--scope");
  foundry_process_launcher_append_argv (launcher, "--collect");
  foundry_process_launcher_append_argv (launcher, "--quiet");
  foundry_process_launcher_append_argv (launcher, "--same-dir");

  uuid = g_uuid_string_random ();
  foundry_process_launcher_append_formatted (launcher, "--unit=foundry-%s.scope", uuid);

  /* Handle PATH specially to apply pipeline prepend/append paths */
  if (env != NULL)
    {
      for (guint i = 0; env[i]; i++)
        {
          if (g_str_has_prefix (env[i], "PATH="))
            {
              path = env[i] + 5; /* Skip "PATH=" */
              break;
            }
        }
    }

  if (prepare->pipeline != NULL)
    {
      pipeline_prepend = foundry_build_pipeline_dup_prepend_path (prepare->pipeline);
      pipeline_append = foundry_build_pipeline_dup_append_path (prepare->pipeline);
    }

  if (path != NULL || pipeline_prepend != NULL || pipeline_append != NULL)
    {
      g_autofree char *tmp = NULL;

      if (path == NULL)
        path = foundry_shell_get_default_path ();

      tmp = foundry_search_path_prepend (path, pipeline_prepend);
      new_path = foundry_search_path_append (tmp, pipeline_append);
      foundry_process_launcher_append_formatted (launcher, "--setenv=PATH=%s", new_path);
    }

  if (env != NULL)
    {
      for (guint i = 0; env[i]; i++)
        {
          if (!g_str_has_prefix (env[i], "PATH="))
            foundry_process_launcher_append_formatted (launcher, "--setenv=%s", env[i]);
        }
    }

  foundry_process_launcher_append_args (launcher, argv);

  return TRUE;
}

static DexFuture *
plugin_host_sdk_prepare_to_build (FoundrySdk                *sdk,
                                  FoundryBuildPipeline      *pipeline,
                                  FoundryProcessLauncher    *launcher,
                                  FoundryBuildPipelinePhase  phase)
{
  PluginHostSdk *self = (PluginHostSdk *)sdk;
  HostSdkPrepare *prepare = NULL;

  g_assert (PLUGIN_IS_HOST_SDK (self));
  g_assert (!pipeline || FOUNDRY_IS_BUILD_PIPELINE (pipeline));
  g_assert (FOUNDRY_IS_PROCESS_LAUNCHER (launcher));

  foundry_process_launcher_push_host (launcher);

  if (self->systemd_run_path != NULL)
    {
      prepare = g_new0 (HostSdkPrepare, 1);
      prepare->self = g_object_ref (self);
      if (pipeline != NULL)
        prepare->pipeline = g_object_ref (pipeline);

      foundry_process_launcher_push (launcher,
                                     plugin_host_sdk_systemd_run_handler,
                                     prepare,
                                     (GDestroyNotify) host_sdk_prepare_free);
    }

  foundry_process_launcher_add_minimal_environment (launcher);

  return dex_future_new_true ();
}

static DexFuture *
plugin_host_sdk_prepare_to_run (FoundrySdk             *sdk,
                                FoundryBuildPipeline   *pipeline,
                                FoundryProcessLauncher *launcher)
{
  PluginHostSdk *self = (PluginHostSdk *)sdk;
  HostSdkPrepare *prepare = NULL;

  g_assert (PLUGIN_IS_HOST_SDK (self));
  g_assert (!pipeline || FOUNDRY_IS_BUILD_PIPELINE (pipeline));
  g_assert (FOUNDRY_IS_PROCESS_LAUNCHER (launcher));

  foundry_process_launcher_push_host (launcher);

  if (self->systemd_run_path != NULL)
    {
      prepare = g_new0 (HostSdkPrepare, 1);
      prepare->self = g_object_ref (self);
      if (pipeline != NULL)
        prepare->pipeline = g_object_ref (pipeline);

      foundry_process_launcher_push (launcher,
                                     plugin_host_sdk_systemd_run_handler,
                                     prepare,
                                     (GDestroyNotify) host_sdk_prepare_free);
    }

  foundry_process_launcher_add_minimal_environment (launcher);

  return dex_future_new_true ();
}

static DexFuture *
plugin_host_sdk_translate_path (FoundrySdk           *sdk,
                                FoundryBuildPipeline *pipeline,
                                const char           *path)
{
  GFile *file;

  if (_foundry_in_container ())
    file = g_file_new_build_filename ("/var/run/host", path, NULL);
  else
    file = g_file_new_for_path (path);

  return dex_future_new_take_object (g_steal_pointer (&file));
}

static void
plugin_host_sdk_finalize (GObject *object)
{
  PluginHostSdk *self = (PluginHostSdk *)object;

  g_clear_pointer (&self->systemd_run_path, g_free);

  G_OBJECT_CLASS (plugin_host_sdk_parent_class)->finalize (object);
}

static void
plugin_host_sdk_class_init (PluginHostSdkClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  FoundrySdkClass *sdk_class = FOUNDRY_SDK_CLASS (klass);

  object_class->finalize = plugin_host_sdk_finalize;

  sdk_class->prepare_to_build = plugin_host_sdk_prepare_to_build;
  sdk_class->prepare_to_run = plugin_host_sdk_prepare_to_run;
  sdk_class->translate_path = plugin_host_sdk_translate_path;
}

static void
plugin_host_sdk_init (PluginHostSdk *self)
{
  self->in_flatpak = g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS);
}

FoundrySdk *
plugin_host_sdk_new (FoundryContext *context,
                     const char     *systemd_run_path)
{
  PluginHostSdk *self;

  g_return_val_if_fail (FOUNDRY_IS_CONTEXT (context), NULL);

  self = g_object_new (PLUGIN_TYPE_HOST_SDK,
                       "context", context,
                       "id", "host",
                       "arch", foundry_get_default_arch (),
                       "name", _("My Computer"),
                       "kind", "host",
                       "installed", TRUE,
                       NULL);
  g_set_str (&self->systemd_run_path, systemd_run_path);

  return FOUNDRY_SDK (self);
}

char *
plugin_host_sdk_build_filename (PluginHostSdk  *self,
                                const char     *first_element,
                                ...)
{
  g_autofree char *joined = NULL;
  va_list args;

  va_start (args, first_element);
  joined = g_build_filename_valist (first_element, &args);
  va_end (args);

  if (self->in_flatpak)
    return g_build_filename ("/var/run/host", joined, NULL);

  if (g_path_is_absolute (joined))
    return g_steal_pointer (&joined);

  return g_build_filename ("/", joined, NULL);
}