on master help sign in
Add custom AppRun.

Fixes #8.

commit2c56cec7397c2cb42e0fdd402b60b0e2a794be76 authorMatthew Bauer <mjbauer95@gmail.com> dateSun 30 Apr 2017, 18:50 (9 years ago) parentabc23a5
2 files changed+285−6
AppRun.c+269 appdir.nix+16−6
AppRun.cnew file+269
@@ -0,0 +1,269 @@
1+/**************************************************************************
2+
3+Copyright (c) 2004-16 Simon Peter
4+Portions Copyright (c) 2010 RazZziel
5+
6+All Rights Reserved.
7+
8+Permission is hereby granted, free of charge, to any person obtaining a copy
9+of this software and associated documentation files (the "Software"), to deal
10+in the Software without restriction, including without limitation the rights
11+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+copies of the Software, and to permit persons to whom the Software is
13+furnished to do so, subject to the following conditions:
14+
15+The above copyright notice and this permission notice shall be included in
16+all copies or substantial portions of the Software.
17+
18+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+THE SOFTWARE.
25+
26+**************************************************************************/
27+
28+#define _GNU_SOURCE
29+#include <stdio.h>
30+#include <stdlib.h>
31+#include <unistd.h>
32+#include <libgen.h>
33+#include <dirent.h>
34+#include <string.h>
35+#include <sys/stat.h>
36+#include <sched.h>
37+#include <fcntl.h>
38+#include <sys/mount.h>
39+#include <errno.h>
40+
41+#define die(...) \
42+ do { \
43+ fprintf(stderr, "Error: " __VA_ARGS__); \
44+ exit(1); \
45+ } while (0);
46+
47+#define PATH_MAX 4096
48+
49+#define LINE_SIZE 255
50+
51+#define err_exit(format, ...) { fprintf(stderr, format ": %s\n", ##__VA_ARGS__, strerror(errno)); exit(EXIT_FAILURE); }
52+
53+int filter (const struct dirent *dir) {
54+ char *p = (char*) &dir->d_name;
55+ p = strrchr(p, '.');
56+ return p && !strcmp(p, ".desktop");
57+}
58+
59+static void update_map(char *mapping, char *map_file) {
60+ int fd;
61+
62+ fd = open(map_file, O_WRONLY);
63+ if (fd < 0) {
64+ err_exit("map open");
65+ }
66+
67+ int map_len = strlen(mapping);
68+ if (write(fd, mapping, map_len) != map_len) {
69+ err_exit("map write");
70+ }
71+
72+ close(fd);
73+}
74+
75+static void add_path(const char* name, const char* rootdir) {
76+ char path_buf[PATH_MAX];
77+ snprintf(path_buf, sizeof(path_buf), "/%s", name);
78+
79+ struct stat statbuf;
80+ if (stat(path_buf, &statbuf) < 0) {
81+ fprintf(stderr, "Cannot stat %s: %s\n", path_buf, strerror(errno));
82+ return;
83+ }
84+
85+ char path_buf2[PATH_MAX];
86+ snprintf(path_buf2, sizeof(path_buf2), "%s/%s", rootdir, name);
87+
88+ mkdir(path_buf2, statbuf.st_mode & ~S_IFMT);
89+ if (mount(path_buf, path_buf2, "none", MS_BIND | MS_REC, NULL) < 0) {
90+ fprintf(stderr, "Cannot bind mount %s to %s: %s\n", path_buf, path_buf2, strerror(errno));
91+ }
92+}
93+
94+#define SAVE_ENV_VAR(x) char *x = getenv(#x)
95+#define LOAD_ENV_VAR(x) setenv(#x, x, 1)
96+
97+int main(int argc, char *argv[]) {
98+ char *appdir = dirname(realpath("/proc/self/exe", NULL));
99+ if (!appdir)
100+ die("Could not access /proc/self/exe\n");
101+
102+ char *tmpdir = getenv("TMPDIR");
103+ if (!tmpdir) {
104+ tmpdir = "/tmp";
105+ }
106+
107+ char template[PATH_MAX];
108+ int needed = snprintf(template, PATH_MAX, "%s/nixXXXXXX", tmpdir);
109+ if (needed < 0) {
110+ err_exit("TMPDIR too long: '%s'", tmpdir);
111+ }
112+
113+ char *rootdir = mkdtemp(template);
114+ if (!rootdir) {
115+ err_exit("mkdtemp(%s)", template);
116+ }
117+
118+ int ret;
119+
120+ struct dirent **namelist;
121+
122+ ret = scandir(appdir, &namelist, filter, NULL);
123+
124+ if (ret == 0) {
125+ die("No .desktop files found\n");
126+ } else if(ret == -1) {
127+ die("Could not scan directory %s\n", appdir);
128+ }
129+
130+ /* Extract executable from .desktop file */
131+
132+ FILE *f;
133+ char *desktop_file = malloc(LINE_SIZE);
134+ snprintf(desktop_file, LINE_SIZE-1, "%s/%s", appdir, namelist[0]->d_name);
135+ f = fopen(desktop_file, "r");
136+
137+ char *line = malloc(LINE_SIZE);
138+ size_t n = LINE_SIZE;
139+ int found = 0;
140+
141+ while (getline(&line, &n, f) != -1)
142+ {
143+ if (!strncmp(line,"Exec=",5))
144+ {
145+ char *p = line+5;
146+ while (*++p && *p != ' ' && *p != '%' && *p != '\n');
147+ *p = 0;
148+ found = 1;
149+ break;
150+ }
151+ }
152+
153+ fclose(f);
154+
155+ if (!found)
156+ die("Executable not found, make sure there is a line starting with 'Exec='\n");
157+
158+ /* Execution */
159+ char *executable = basename(line+5);
160+
161+ char full_exec[PATH_MAX];
162+ snprintf(full_exec, sizeof(full_exec), "/usr/bin/%s", executable);
163+
164+ // get uid, gid before going to new namespace
165+ uid_t uid = getuid();
166+ gid_t gid = getgid();
167+
168+ // "unshare" into new namespace
169+ if (unshare(CLONE_NEWNS | CLONE_NEWUSER) < 0) {
170+ err_exit("unshare()");
171+ }
172+
173+ // add necessary system stuff to rootdir namespace
174+ add_path("dev", rootdir);
175+ add_path("proc", rootdir);
176+ add_path("sys", rootdir);
177+ add_path("run", rootdir);
178+ add_path("etc", rootdir);
179+
180+ char path_buf[PATH_MAX];
181+ snprintf(path_buf, sizeof(path_buf), "%s/tmp", rootdir);
182+ mkdir(path_buf, ~0);
183+ snprintf(path_buf, sizeof(path_buf), "%s/var", rootdir);
184+ mkdir(path_buf, ~0);
185+
186+ // make sure nixdir exists
187+ struct stat statbuf2;
188+ if (stat(appdir, &statbuf2) < 0) {
189+ err_exit("stat(%s)", appdir);
190+ }
191+
192+ char nixdir[PATH_MAX];
193+ snprintf(nixdir, sizeof(nixdir), "%s/nix", appdir);
194+ snprintf(path_buf, sizeof(path_buf), "%s/nix", rootdir);
195+ mkdir(path_buf, statbuf2.st_mode & ~S_IFMT);
196+ if (mount(nixdir, path_buf, "none", MS_BIND | MS_REC, NULL) < 0) {
197+ err_exit("mount(%s, %s)", nixdir, path_buf);
198+ }
199+
200+ char usrdir[PATH_MAX];
201+ snprintf(usrdir, sizeof(usrdir), "%s/usr", appdir);
202+ snprintf(path_buf, sizeof(path_buf), "%s/usr", rootdir);
203+ mkdir(path_buf, statbuf2.st_mode & ~S_IFMT);
204+ if (mount(usrdir, path_buf, "none", MS_BIND | MS_REC, NULL) < 0) {
205+ err_exit("mount(%s, %s)", usrdir, path_buf);
206+ }
207+
208+ // fixes issue #1 where writing to /proc/self/gid_map fails
209+ // see user_namespaces(7) for more documentation
210+ int fd_setgroups = open("/proc/self/setgroups", O_WRONLY);
211+ if (fd_setgroups > 0) {
212+ write(fd_setgroups, "deny", 4);
213+ }
214+
215+ // map the original uid/gid in the new ns
216+ char map_buf[1024];
217+ snprintf(map_buf, sizeof(map_buf), "%d %d 1", uid, uid);
218+ update_map(map_buf, "/proc/self/uid_map");
219+ snprintf(map_buf, sizeof(map_buf), "%d %d 1", gid, gid);
220+ update_map(map_buf, "/proc/self/gid_map");
221+
222+ // chroot to rootdir
223+ if (chroot(appdir) < 0) {
224+ err_exit("chroot(%s)", appdir);
225+ }
226+
227+ chdir("/");
228+
229+ SAVE_ENV_VAR(DBUS_SESSION_BUS_ADDRESS);
230+ SAVE_ENV_VAR(USER);
231+ SAVE_ENV_VAR(HOSTNAME);
232+ SAVE_ENV_VAR(LANG);
233+ SAVE_ENV_VAR(LC_ALL);
234+ SAVE_ENV_VAR(TERM);
235+ SAVE_ENV_VAR(DISPLAY);
236+ SAVE_ENV_VAR(XDG_RUNTIME_DIR);
237+ SAVE_ENV_VAR(XAUTHORITY);
238+ SAVE_ENV_VAR(XDG_SESSION_ID);
239+ SAVE_ENV_VAR(XDG_SEAT);
240+
241+ clearenv();
242+
243+ LOAD_ENV_VAR(DBUS_SESSION_BUS_ADDRESS);
244+ LOAD_ENV_VAR(USER);
245+ LOAD_ENV_VAR(HOSTNAME);
246+ LOAD_ENV_VAR(LANG);
247+ LOAD_ENV_VAR(LC_ALL);
248+ LOAD_ENV_VAR(TERM);
249+ LOAD_ENV_VAR(DISPLAY);
250+ LOAD_ENV_VAR(XDG_RUNTIME_DIR);
251+ LOAD_ENV_VAR(XAUTHORITY);
252+ LOAD_ENV_VAR(XDG_SESSION_ID);
253+ LOAD_ENV_VAR(XDG_SEAT);
254+
255+ setenv("PATH", "", 1);
256+ setenv("HOME", "/", 1);
257+ setenv("TMPDIR", "/tmp", 1);
258+
259+ /* Run */
260+ // FIXME: What about arguments in the Exec= line of the desktop file?
261+ ret = execvp(full_exec, argv);
262+
263+ if (ret == -1)
264+ die("Error executing '%s'; return code: %d\n", full_exec, ret);
265+
266+ free(line);
267+ free(desktop_file);
268+ return 0;
269+}
appdir.nix+16−6
@@ -1,9 +1,20 @@
1−{ stdenv, fetchurl, perl, pathsFromGraph }:
1+{ stdenv, fetchurl, perl, pathsFromGraph, fetchFromGitHub, musl }:
22
33let
4− AppRun = fetchurl {
5− url = "https://github.com/probonopd/AppImageKit/releases/download/7/AppRun-x86_64";
6− sha256 = "1k2jlyrrbzdmxv832a5vin4nzs3rh9r9vs7a8jzrl1v5bbh16nqf";
4+ AppRun = stdenv.mkDerivation {
5+ name = "AppRun";
6+
7+ phases = [ "buildPhase" "installPhase" "fixupPhase" ];
8+
9+ buildPhase = ''
10+ CC="${musl}/bin/musl-gcc -O2 -Wall -Wno-deprecated-declarations -Wno-unused-result -static"
11+ $CC ${./AppRun.c} -o AppRun
12+ '';
13+
14+ installPhase = ''
15+ mkdir -p $out/bin
16+ cp AppRun $out/bin/AppRun
17+ '';
718 };
819
920in
@@ -63,7 +74,6 @@ in
6374 fi
6475 fi
6576
66− cp ${AppRun} AppRun
67− chmod a+x AppRun
77+ cp ${AppRun}/bin/AppRun AppRun
6878 '';
6979 }