1 /* ide-global.c
2  *
3  * Copyright 2014-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "ide-global"
22 
23 #include "config.h"
24 
25 #include <gio/gio.h>
26 #include <glib/gi18n.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/user.h>
30 #include <sys/utsname.h>
31 #include <unistd.h>
32 
33 #include "../../gconstructor.h"
34 
35 #include "ide-debug.h"
36 #include "ide-global.h"
37 #include "ide-macros.h"
38 #include "ide-private.h"
39 
40 static GThread *main_thread;
41 static const gchar *application_id = "org.gnome.Builder";
42 static IdeProcessKind kind = IDE_PROCESS_KIND_HOST;
43 
44 #if defined (G_HAS_CONSTRUCTORS)
45 # ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
46 #  pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(ide_init_ctor)
47 # endif
G_DEFINE_CONSTRUCTOR(ide_init_ctor)48 G_DEFINE_CONSTRUCTOR(ide_init_ctor)
49 #else
50 # error Your platform/compiler is missing constructor support
51 #endif
52 
53 static void
54 ide_init_ctor (void)
55 {
56   main_thread = g_thread_self ();
57 
58   if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS))
59     kind = IDE_PROCESS_KIND_FLATPAK;
60 }
61 
62 /**
63  * ide_get_main_thread
64  *
65  * Gets #GThread of the main thread.
66  *
67  * Generally this is used by macros to determine what thread they code is
68  * currently running within.
69  *
70  * Returns: (transfer none): a #GThread
71  *
72  * Since: 3.32
73  */
74 GThread *
ide_get_main_thread(void)75 ide_get_main_thread (void)
76 {
77   return main_thread;
78 }
79 
80 /**
81  * ide_get_process_kind:
82  *
83  * Gets the kind of process we're running as.
84  *
85  * Returns: an #IdeProcessKind
86  *
87  * Since: 3.32
88  */
89 IdeProcessKind
ide_get_process_kind(void)90 ide_get_process_kind (void)
91 {
92   return kind;
93 }
94 
95 const gchar *
ide_get_application_id(void)96 ide_get_application_id (void)
97 {
98   return application_id;
99 }
100 
101 /**
102  * ide_set_application_id:
103  * @app_id: the application id
104  *
105  * Sets the application id that will be used.
106  *
107  * This must be set at application startup before any GApplication
108  * has connected to the D-Bus.
109  *
110  * The default is "org.gnome.Builder".
111  *
112  * Since: 3.32
113  */
114 void
ide_set_application_id(const gchar * app_id)115 ide_set_application_id (const gchar *app_id)
116 {
117   g_return_if_fail (app_id != NULL);
118 
119   application_id = g_intern_string (app_id);
120 }
121 
122 const gchar *
ide_get_program_name(void)123 ide_get_program_name (void)
124 {
125   return "gnome-builder";
126 }
127 
128 gchar *
ide_create_host_triplet(const gchar * arch,const gchar * kernel,const gchar * system)129 ide_create_host_triplet (const gchar *arch,
130                          const gchar *kernel,
131                          const gchar *system)
132 {
133   if (arch == NULL || kernel == NULL)
134     return g_strdup (ide_get_system_type ());
135   else if (system == NULL)
136     return g_strdup_printf ("%s-%s", arch, kernel);
137   else
138     return g_strdup_printf ("%s-%s-%s", arch, kernel, system);
139 }
140 
141 const gchar *
ide_get_system_type(void)142 ide_get_system_type (void)
143 {
144   static gchar *system_type;
145   g_autofree gchar *os_lower = NULL;
146   const gchar *machine = NULL;
147   struct utsname u;
148 
149   if (system_type != NULL)
150     return system_type;
151 
152   if (uname (&u) < 0)
153     return g_strdup ("unknown");
154 
155   os_lower = g_utf8_strdown (u.sysname, -1);
156 
157   /* config.sub doesn't accept amd64-OS */
158   machine = strcmp (u.machine, "amd64") ? u.machine : "x86_64";
159 
160   /*
161    * TODO: Clearly we want to discover "gnu", but that should be just fine
162    *       for a default until we try to actually run on something non-gnu.
163    *       Which seems unlikely at the moment. If you run FreeBSD, you can
164    *       probably fix this for me :-) And while you're at it, make the
165    *       uname() call more portable.
166    */
167 
168 #ifdef __GLIBC__
169   system_type = g_strdup_printf ("%s-%s-%s", machine, os_lower, "gnu");
170 #else
171   system_type = g_strdup_printf ("%s-%s", machine, os_lower);
172 #endif
173 
174   return system_type;
175 }
176 
177 gchar *
ide_get_system_arch(void)178 ide_get_system_arch (void)
179 {
180   static GHashTable *remap;
181   const char *machine;
182   struct utsname u;
183 
184   if (uname (&u) < 0)
185     return g_strdup ("unknown");
186 
187   if (g_once_init_enter (&remap))
188     {
189       GHashTable *mapping;
190 
191       mapping = g_hash_table_new (g_str_hash, g_str_equal);
192       g_hash_table_insert (mapping, (gchar *)"amd64", (gchar *)"x86_64");
193       g_hash_table_insert (mapping, (gchar *)"armv7l", (gchar *)"aarch64");
194       g_hash_table_insert (mapping, (gchar *)"i686", (gchar *)"i386");
195 
196       g_once_init_leave (&remap, mapping);
197     }
198 
199   if (g_hash_table_lookup_extended (remap, u.machine, NULL, (gpointer *)&machine))
200     return g_strdup (machine);
201   else
202     return g_strdup (u.machine);
203 }
204 
205 gsize
ide_get_system_page_size(void)206 ide_get_system_page_size (void)
207 {
208   return sysconf (_SC_PAGE_SIZE);
209 }
210 
211 static gchar *
get_base_path(const gchar * name)212 get_base_path (const gchar *name)
213 {
214   g_autoptr(GKeyFile) keyfile = g_key_file_new ();
215 
216   if (g_key_file_load_from_file (keyfile, "/.flatpak-info", 0, NULL))
217     return g_key_file_get_string (keyfile, "Instance", name, NULL);
218 
219   return NULL;
220 }
221 
222 /**
223  * ide_get_relocatable_path:
224  * @path: a relocatable path
225  *
226  * Gets the path to a resource that may be relocatable at runtime.
227  *
228  * Returns: (transfer full): a new string containing the path
229  *
230  * Since: 3.32
231  */
232 gchar *
ide_get_relocatable_path(const gchar * path)233 ide_get_relocatable_path (const gchar *path)
234 {
235   static gchar *base_path;
236 
237   if G_UNLIKELY (base_path == NULL)
238     base_path = get_base_path ("app-path");
239 
240   return g_build_filename (base_path, path, NULL);
241 }
242 
243 const gchar *
ide_gettext(const gchar * message)244 ide_gettext (const gchar *message)
245 {
246   if (message != NULL)
247     return g_dgettext (GETTEXT_PACKAGE, message);
248   return NULL;
249 }
250 
251 static IdeTraceVTable trace_vtable;
252 
253 void
_ide_trace_init(IdeTraceVTable * vtable)254 _ide_trace_init (IdeTraceVTable *vtable)
255 {
256   trace_vtable = *vtable;
257   if (trace_vtable.load)
258     trace_vtable.load ();
259 }
260 
261 void
_ide_trace_shutdown(void)262 _ide_trace_shutdown (void)
263 {
264   if (trace_vtable.unload)
265     trace_vtable.unload ();
266   memset (&trace_vtable, 0, sizeof trace_vtable);
267 }
268 
269 #ifdef IDE_ENABLE_TRACE
270 void
ide_trace_function(const gchar * strfunc,gint64 begin_time_usec,gint64 end_time_usec)271 ide_trace_function (const gchar *strfunc,
272                     gint64       begin_time_usec,
273                     gint64       end_time_usec)
274 {
275   /* In case our clock is not reliable */
276   if (end_time_usec < begin_time_usec)
277     end_time_usec = begin_time_usec;
278 
279   if (trace_vtable.function)
280     trace_vtable.function (strfunc, begin_time_usec, end_time_usec);
281 }
282 #endif
283 
284 void
_ide_trace_log(GLogLevelFlags log_level,const gchar * domain,const gchar * message)285 _ide_trace_log (GLogLevelFlags  log_level,
286                 const gchar    *domain,
287                 const gchar    *message)
288 {
289   if (trace_vtable.log)
290     trace_vtable.log (log_level, domain, message);
291 }
292 
293 static gchar **
get_environ_from_stdout(GSubprocess * subprocess)294 get_environ_from_stdout (GSubprocess *subprocess)
295 {
296   g_autofree gchar *stdout_buf = NULL;
297 
298   if (g_subprocess_communicate_utf8 (subprocess, NULL, NULL, &stdout_buf, NULL, NULL))
299     {
300       g_auto(GStrv) lines = g_strsplit (stdout_buf, "\n", 0);
301       g_autoptr(GPtrArray) env = g_ptr_array_new_with_free_func (g_free);
302 
303       for (guint i = 0; lines[i]; i++)
304         {
305           const char *line = lines[i];
306 
307           if (!g_ascii_isalpha (*line) && *line != '_')
308             continue;
309 
310           for (const char *iter = line; *iter; iter = g_utf8_next_char (iter))
311             {
312               if (*iter == '=')
313                 {
314                   g_ptr_array_add (env, g_strdup (line));
315                   break;
316                 }
317 
318               if (!g_ascii_isalnum (*iter) && *iter != '_')
319                 break;
320             }
321         }
322 
323       if (env->len > 0)
324         {
325           g_ptr_array_add (env, NULL);
326           return (gchar **)g_ptr_array_free (g_steal_pointer (&env), FALSE);
327         }
328     }
329 
330   return NULL;
331 }
332 
333 const gchar * const *
_ide_host_environ(void)334 _ide_host_environ (void)
335 {
336   static gchar **host_environ;
337 
338   if (host_environ == NULL)
339     {
340       if (ide_is_flatpak ())
341         {
342           g_autoptr(GSubprocessLauncher) launcher = NULL;
343           g_autoptr(GSubprocess) subprocess = NULL;
344           g_autoptr(GError) error = NULL;
345 
346           launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
347           subprocess = g_subprocess_launcher_spawn (launcher, &error,
348                                                     "flatpak-spawn", "--host", "printenv", NULL);
349           if (subprocess != NULL)
350             host_environ = get_environ_from_stdout (subprocess);
351         }
352 
353       if (host_environ == NULL)
354         host_environ = g_get_environ ();
355     }
356 
357   return (const char * const *)host_environ;
358 }
359