]> pd.if.org Git - zpackage/blob - lib/zpm.c
55118419b705090d928f280d2842a00ff61d3465
[zpackage] / lib / zpm.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/mman.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <errno.h>
12
13 #include "zpm.h"
14 #include "elf.h"
15
16 #include "sha256.h"
17
18 int zpm_begin(struct zpm *z) {
19         char *errstr = 0;
20         sqlite3_exec(z->db, "begin;", NULL, NULL, &errstr);
21         if (errstr) {
22                 fprintf(stderr, "sqlite begin error: %s\n", errstr);
23                 sqlite3_free(errstr);
24                 return 0;
25         }
26         return 1;
27 }
28
29 int zpm_commit(struct zpm *z) {
30         char *errstr = 0;
31         sqlite3_exec(z->db, "commit;", NULL, NULL, &errstr);
32         if (errstr) {
33                 fprintf(stderr, "sqlite commit error: %s\n", errstr);
34                 sqlite3_free(errstr);
35                 return 0;
36         }
37         return 1;
38 }
39
40 /* wrapper for sqlite3_exec */
41 int zpm_exec(struct zpm *z, const char *sql, int(*callback)(void *, int, char **, char**), void *arg, char **errmsg) {
42         return sqlite3_exec(z->db, sql, callback, arg, errmsg);
43 }
44
45 int zpm_rollback(struct zpm *z) {
46         char *errstr = 0;
47         sqlite3_exec(z->db, "rollback;", NULL, NULL, &errstr);
48         if (errstr) {
49                 fprintf(stderr, "sqlite rollback error: %s\n", errstr);
50                 sqlite3_free(errstr);
51                 return 0;
52         }
53         return 1;
54 }
55
56 int zpm_db_set_pragma(struct zpm *db, int pragma, int value) {
57         int rc;
58         char *sql;
59         sqlite3_stmt *s;
60
61         switch (pragma) {
62                 case 1: sql = "pragma application_id = ?;"; break;
63                 case 2: sql = "pragma user_version = ?;"; break;
64                 default: return -1; break;
65         }
66
67         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
68
69         if (rc != SQLITE_OK) {
70                 SQLERROR(sqlite3_errmsg(db->db));
71                 return -1;
72         }
73
74         sqlite3_bind_int(s, 1, value);
75         if (rc != SQLITE_OK) {
76                 SQLERROR(sqlite3_errmsg(db->db));
77                 fprintf(stderr, "cant bind pragma value\n");
78                 sqlite3_finalize(s);
79                 return -1;
80         }
81         rc = sqlite3_step(s);
82         if (rc != SQLITE_DONE) {
83                 SQLERROR(sqlite3_errmsg(db->db));
84                 fprintf(stderr, "cant set pragma\n");
85                 sqlite3_finalize(s);
86                 return -1;
87         }
88
89         sqlite3_finalize(s);
90         return value;
91 }
92
93 int zpm_db_pragma(struct zpm *db, int pragma) {
94         int rc;
95         int value = -1;
96         char *sql = 0;
97         sqlite3_stmt *s;
98
99         switch (pragma) {
100                 case 1: sql = "pragma application_id;"; break;
101                 case 2: sql = "pragma user_version;"; break;
102                 default: return -1; break;
103         }
104
105         rc = sqlite3_prepare_v2(db->db, sql, -1, &s, 0);
106
107         if (rc != SQLITE_OK) {
108                 SQLERROR(sqlite3_errmsg(db->db));
109                 fprintf(stderr, "%s, errnum = %d\n", sqlite3_errmsg(db->db), rc);
110                 /* TODO just abort? */
111                 return -1;
112         }
113
114         rc = sqlite3_step(s);
115         if (rc == SQLITE_ROW) {
116                 value = sqlite3_column_int(s, 0);
117         }
118
119         sqlite3_finalize(s);
120         return value;
121 }
122
123 static
124 #include "newdb.c"
125
126 int zpm_db_initialize(struct zpm *pkg) {
127         char *error;
128         switch (sqlite3_exec(pkg->db, createdb, (int (*)(void *,int,char **,char **))0, NULL, &error)) {
129                 case SQLITE_OK: break;
130                 default:
131                         SQLERROR(sqlite3_errmsg(pkg->db));
132                         fprintf(stderr, "error: %s\n", error);
133                         sqlite3_free(error);
134                         zpm_rollback(pkg);
135                         return 0;
136                         break;
137         }
138         return 1;
139 }
140
141
142 /* assumes pkg->db is set */
143 static int setupdb(struct zpm *zpm) {
144         char *errstr = 0;
145         int appid, dbver;
146
147         zpm->error = 0;
148
149         appid = zpm_db_pragma(zpm, 1);
150         dbver = zpm_db_pragma(zpm, 2);
151
152         if (appid != 0x5a504442) {
153                 fprintf(stderr, "unknown database type\n");
154                 zpm->error = 1;
155                 return 0;
156         }
157
158         if (dbver > 1) {
159                 fprintf(stderr, "version %d zpm db detected, this program only works with version 1 databases\n", dbver);
160                 zpm->error = 1;
161                 return 0;
162         }
163
164         sqlite3_exec(zpm->db, "pragma foreign_keys = ON;", NULL, NULL, &errstr);
165
166         if (errstr) {
167                 free(zpm->errmsg);
168                 zpm->errmsg = strdup(errstr);
169                 fprintf(stderr, "sqlite foreign key error: %s\n", errstr);
170                 sqlite3_free(errstr);
171                 zpm->error = 1;
172                 return 0;
173         }
174
175         /* TODO add vercmp */
176
177         return 1;
178 }
179
180 struct zpm *zpm_clearmem(struct zpm *zpm) {
181         if (!zpm) {
182                 zpm = malloc(sizeof *zpm);
183         }
184
185         if (zpm) {
186                 *zpm = (struct zpm){0};
187         }
188
189         return zpm;
190 }
191
192 static void zpm_set_db_errmsg(struct zpm *zpm, const char *msg) {
193         if (zpm) {
194                 if (zpm->dberrmsg) {
195                         free(zpm->dberrmsg);
196                 }
197                 if (msg) {
198                         zpm->dberrmsg = strdup(msg);
199                         if (!zpm->dberrmsg) {
200                                 zpm->error = 1;
201                         }
202                 } else {
203                         zpm->dberrmsg = 0;
204                 }
205         }
206 }
207
208 int zpm_init(struct zpm *pkg, char *path) {
209         int rc;
210         sqlite3 *db = 0;
211         int appid;
212
213         if (!pkg) {
214                 return 0;
215         }
216
217         zpm_clearmem(pkg);
218
219         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
220         if (rc) {
221                 pkg->error = 1;
222                 pkg->dbresult = rc;
223                 zpm_set_db_errmsg(pkg, sqlite3_errstr(rc));
224                 if (db) {
225                         sqlite3_close(db);
226                 }
227                 fprintf(stderr, "error (%d): %s: %s\n", rc,
228                                 pkg->dberrmsg ? pkg->dberrmsg : "null", path);
229
230                 return 0;
231         }
232         pkg->db   = db;
233         pkg->path = strdup(path);
234
235         appid = zpm_db_pragma(pkg, 1);
236
237         switch (appid) {
238                 case 0: if (!zpm_db_initialize(pkg)) {
239                                 sqlite3_close(db);
240                                 return 1;
241                         };
242                         break;
243                 case 0x5a504442:
244                         /* already initialized */
245                         break;
246                 default:
247                         fprintf(stderr, "unknown database type\n");
248                         sqlite3_close(db);
249                         return 0;
250                         break;
251         }
252
253         if (!setupdb(pkg)) {
254                 sqlite3_close(db);
255                 pkg->db = 0;
256                 return 0;
257         }
258         zpm_addvercmp(pkg);
259
260         return 1;
261 }
262
263 int zpm_open(struct zpm *zpm, char *path) {
264         int rc;
265         sqlite3 *db = 0;
266
267         zpm_clearmem(zpm);
268
269         rc = sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL);
270         if (rc) {
271                 SQLERROR(sqlite3_errmsg(db));
272                 sqlite3_close(db);
273                 zpm->error = 1;
274                 fprintf(stderr, "path = %s\n", path);
275                 return 0;
276         }
277         zpm->db   = db;
278         zpm->path = strdup(path);
279
280         if (!setupdb(zpm)) {
281                 sqlite3_close(db);
282                 zpm->db = 0;
283                 zpm->error = 1;
284                 return 0;
285         }
286         zpm_addvercmp(zpm);
287
288         return 1;
289 }
290
291 int zpm_close(struct zpm *pkg) {
292         if (pkg) {
293                 sqlite3_close(pkg->db);
294                 free(pkg->path);
295         }
296         /* TODO free any malloced names and such */
297         return 1;
298 }
299
300 static int zpm_sqlite_vercmp(void *not_used, int lena, const void *a,
301                 int lenb, const void *b) {
302         /* not sure what the ints are, possibly string lengths */
303         if (not_used != 0) fprintf(stderr, "sqlite vercmp not_used = %p\n",
304                         not_used);
305         if (lena == 0 && lenb > 0) return 1;
306         return zpm_vercmp(a, b);
307 }
308
309 int zpm_addvercmp(struct zpm *pkg) {
310         return sqlite3_create_collation(
311                         pkg->db, "vercmp", SQLITE_UTF8, NULL,
312                         zpm_sqlite_vercmp
313                         );
314 }
315
316 int zpm_extract(struct zpm *pkg, char *hash, char *path, mode_t mode) {
317         int rc;
318         int blobsize;
319         void *xzdata;
320         int type;
321         int out;
322         sqlite3_stmt *ifile;
323         size_t len;
324         char *tmpfile = 0;
325         sqlite3 *db;
326
327         if (!pkg || !pkg->db) {
328                 return 0;
329         }
330         db = pkg->db;
331
332         rc = sqlite3_prepare(db, "select size, content from files where hash = ?", -1, &ifile,0);
333         if (rc != SQLITE_OK) {
334                 SQLERROR(sqlite3_errmsg(db));
335                 return 0;
336         }
337
338         /* hash, filename */
339
340         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
341
342         rc = sqlite3_step(ifile);
343
344         if (rc == SQLITE_DONE) {
345                 /* didn't find a row */
346                 sqlite3_finalize(ifile);
347                 sqlite3_close(db);
348                 fprintf(stderr, "no such hash: %s\n", hash);
349                 return 0;
350         }
351         /* either way we're done with this now */
352
353         if (rc != SQLITE_ROW) {
354                 SQLERROR(sqlite3_errmsg(db));
355                 sqlite3_finalize(ifile);
356                 sqlite3_close(db);
357                 return 0;
358         }
359
360         type = sqlite3_column_type(ifile, 0);
361         if (type == SQLITE_NULL) {
362                 fprintf(stderr, "no file size\n");
363                 sqlite3_finalize(ifile);
364                 sqlite3_close(db);
365                 return 0;
366         }
367         type = sqlite3_column_type(ifile, 1);
368         if (type == SQLITE_NULL) {
369                 fprintf(stderr, "no file data\n");
370                 sqlite3_finalize(ifile);
371                 sqlite3_close(db);
372                 return 0;
373         }
374         //size = sqlite3_column_int64(ifile, 0);
375         xzdata = (void *)sqlite3_column_blob(ifile, 1);
376         blobsize = sqlite3_column_bytes(ifile, 1);
377
378         if (strcmp(path, "-")) {
379                 len = strlen(path);
380                 tmpfile = malloc(len+8+1);
381                 if (!tmpfile) {
382                         fprintf(stderr, "malloc error\n");
383                         return 0;
384                 }
385                 sprintf(tmpfile, "%sXXXXXX", path);
386
387                 out = open(tmpfile, O_CREAT|O_WRONLY|O_TRUNC, 0600);
388                 if (out == -1) {
389                         fprintf(stderr, "can't open output file %s: %s\n",
390                                         tmpfile, strerror(errno));
391                         sqlite3_finalize(ifile);
392                         sqlite3_close(db);
393                         return 0;
394                 }
395         } else {
396                 out = 1;
397         }
398
399 #if 0
400         fprintf(stderr, "uncompressing %d bytes at %p, expect %lld\n", blobsize, xzdata, (long long int)size);
401 #endif
402         uncompresslzma(xzdata, blobsize, out);
403         close(out);
404         sqlite3_finalize(ifile);
405
406         rc = 1;
407         if (tmpfile) {
408                 if (chmod(tmpfile, mode) == -1) {
409                         fprintf(stderr, "can't chmod %s: %s\n", tmpfile,
410                                         strerror(errno));
411                         rc = 0;
412                 } else if (rename(tmpfile, path) == -1) {
413                         fprintf(stderr, "extract rename failed: %s\n",
414                                         strerror(errno));
415                         rc = 0;
416                 }
417         }
418
419         if (rc == 0 && tmpfile) {
420                 if (unlink(tmpfile) == -1) {
421                         fprintf(stderr, "unlink tmpfile %s fail: %s\n",
422                                         tmpfile,
423                                         strerror(errno));
424                 }
425         }
426
427         return rc;
428 }
429
430 static int run_for_hash(sqlite3 *db, char *sql, char *hash) {
431         int rc;
432         sqlite3_stmt *ifile;
433
434         rc = sqlite3_prepare_v2(db, sql, -1, &ifile, 0);
435         if (rc != SQLITE_OK) {
436                 SQLERROR(sqlite3_errmsg(db));
437                 return 0;
438         }
439
440         /* hash, filename */
441
442         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
443         do {
444                 rc = sqlite3_step(ifile);
445         } while (rc == SQLITE_ROW);
446
447         sqlite3_finalize(ifile);
448
449         return rc == SQLITE_DONE;
450 }
451
452 #define SQLERP(db, msg) fprintf(stderr, "%s: %s\n", msg, sqlite3_errmsg(db))
453
454 static int set_elf_info(struct zpm *zpm, char *hash, char *content,
455                 size_t length) {
456
457         char *strtab;
458         Elf64_Dyn *dyn;
459         int i;
460         Elf64_Phdr *phdr;
461         Elf64_Ehdr *hdr;
462         sqlite3_stmt *ifile;
463         sqlite3 *db = zpm->db;
464
465         if (length < sizeof (Elf64_Ehdr) || !libelf_iself(content)) {
466                 /* not an elf file */
467                 return 1;
468         }
469
470         /* clear existing for this hash */
471
472         if (!run_for_hash(db, "delete from elflibraries where file = ?", hash)) {
473                 SQLERP(db, "error clearing elf library");
474                 return 0;
475         }
476
477         if (!run_for_hash(db, "delete from elfneeded where file = ?", hash)) {
478                 SQLERP(db, "error clearing elf needed");
479                 return 0;
480         }
481
482         hdr = libelf_header(content);
483         /* if lib, set soname */
484         if (libelf_type(content) == ET_DYN) {
485                 char *soname = libelf_soname(content);
486                 if (soname) {
487                         zpm_db_run(zpm, "insert into elflibraries (file,soname) values (%Q,%Q);", hash, soname);
488                         if (zpm->error) {
489                                 return 0;
490                         }
491                 } else {
492                         fprintf(stderr, "can't find soname\n");
493                 }
494         }
495
496         /* if exe, set neededs */
497         /* libraries can have neededs too */
498         if (libelf_type(content) == ET_EXEC || libelf_type(content) == ET_DYN) {
499                 Elf64_Shdr *dsect = 0;
500                 char *elf;
501
502                 elf = (char *)content;
503                 /* find program header table */
504                 for (i = 0; i < hdr->e_phnum; i++) {
505                         phdr = (Elf64_Phdr *)(elf + hdr->e_phoff + i * hdr->e_phentsize);
506                         if (phdr->p_type == PT_DYNAMIC) {
507                                 dsect = (Elf64_Shdr *)(elf + phdr->p_offset);
508                         }
509                 }
510                 if (!dsect) {
511                         /* no dynamic section found */
512                         return 1;
513                 }
514
515                 dyn = (Elf64_Dyn *)dsect;
516
517                 dsect = libelf_section(elf, SHT_DYNAMIC);
518                 Elf64_Shdr *strsect;
519
520                 strsect = libelf_section_n(elf, dsect->sh_link);
521                 strtab = elf + strsect->sh_offset;
522
523                 sqlite3_prepare_v2(db, "insert into elfneeded (file,needed) values (?,?)",-1, &ifile, 0);
524                 sqlite3_bind_text(ifile,1,hash,64,SQLITE_STATIC);
525                 while (dyn->d_tag != DT_NULL) {
526                         if (dyn->d_tag == DT_NEEDED) {
527                                 char *need;
528                                 int rc;
529
530                                 need = strtab + dyn->d_un.d_val;
531                                 if (strlen(need) == 0) continue;
532                                 sqlite3_bind_text(ifile,2,need,strlen(need),SQLITE_STATIC);
533 #if 0
534                                 fprintf(stderr, "%s needs %s\n", hash, need);
535 #endif
536                                 rc = sqlite3_step(ifile);
537                                 if (rc != SQLITE_DONE) {
538                                         SQLERP(db, "error setting needed library");
539                                         sqlite3_finalize(ifile);
540                                         return 0;
541                                 }
542                                 sqlite3_reset(ifile);
543                         }
544                         dyn++;
545                 }
546                 sqlite3_finalize(ifile);
547         }
548         return 1;
549 }
550
551 int zpm_import(struct zpm *zpm, char *path, uint32_t flags, char *hash) {
552         int fd;
553         void *content = 0;
554         struct stat sbuf;
555         unsigned char tmp[32];
556         struct sha256_state md;
557         sqlite3_stmt *ifile = 0;
558         int haverow = 0,havedata = 0;
559         int j,rc,type;
560         char hashbuf[65];
561
562         /* xz compress it */
563         size_t outlen = 0;
564         void *outbuf;
565
566         if (!zpm || !zpm->db || !path) {
567                 return 0;
568         }
569
570         /* use local if caller didn't pass in room */
571         if (!hash) {
572                 hash = hashbuf;
573         }
574
575         if (flags) {
576                 fprintf(stderr, "zpm_import unused flags = %d\n", flags);
577         }
578         /* mmap the file */
579         fd = open(path, O_RDONLY);
580         if (fd == -1) {
581                 zpm->error = errno;
582                 fprintf(stderr, "%s can't open %s: %s\n", __FUNCTION__, path,strerror(errno));
583                 return 0;
584         }
585         if (fstat(fd, &sbuf) == -1) {
586                 zpm->error = errno;
587                 fprintf(stderr, "%s can't fstat %s: %s\n", __FUNCTION__, path,strerror(errno));
588                 return 0;
589         }
590         /* not a regular file? */
591         if (!S_ISREG(sbuf.st_mode)) {
592                 char *ftype;
593                 switch (sbuf.st_mode & S_IFMT) {
594                         case S_IFSOCK: ftype = "socket"; break;
595                         case S_IFLNK : ftype = "symlink"; break;
596                         case S_IFBLK : ftype = "block device"; break;
597                         case S_IFDIR : ftype = "directory"; break;
598                         case S_IFCHR : ftype = "character device"; break;
599                         case S_IFIFO : ftype = "fifo"; break;
600                         default: ftype = "unknown file type"; break;
601                 }
602                 /* TODO this is ok, just stored differently */
603                 fprintf(stderr, "%s can't import %s file: %s\n", __FUNCTION__, ftype, path);
604                 zpm->error = EINVAL;
605                 return 0;
606         }
607
608         content = mmap(0, sbuf.st_size, PROT_READ,MAP_PRIVATE, fd, 0);
609         close(fd);
610         if (!content) {
611                 zpm->error = errno;
612                 fprintf(stderr, "%s can't mmap %s: %s\n", __FUNCTION__, path,strerror(errno));
613                 return 0;
614         }
615
616         /* get hash */
617         sha256_init(&md);
618         sha256_process(&md, content, sbuf.st_size);
619         sha256_done(&md, tmp);
620         for (j=0;j<32;j++) {
621                 sprintf(hash+j*2, "%02x", (unsigned)tmp[j]);
622         }
623         hash[64] = 0;
624
625         /* TODO check null */
626         sqlite3 *db = zpm->db;
627
628         /* prepare and bind */
629
630         rc = sqlite3_prepare_v2(db, "select size, content is not null from files where hash = ?", -1, &ifile,0);
631         if (rc != SQLITE_OK) {
632                 SQLERROR(sqlite3_errmsg(db));
633                 munmap(content, sbuf.st_size);
634                 return 0;
635         }
636
637         /* hash, filename */
638
639         sqlite3_bind_text(ifile, 1, hash, 64, SQLITE_STATIC);
640
641         rc = sqlite3_step(ifile);
642
643         if (rc != SQLITE_DONE) {
644                 if (rc != SQLITE_ROW) {
645                         /* didn't find a row */
646                         SQLERROR(sqlite3_errmsg(db));
647                 munmap(content, sbuf.st_size);
648                         return 0;
649                 }
650                 haverow = 1;
651                 type = sqlite3_column_type(ifile, 0);
652                 if (type == SQLITE_NULL) {
653                         /* TODO assert, this shouldn't be possible? */
654                         fprintf(stderr, "no file size\n");
655                         sqlite3_finalize(ifile);
656                 munmap(content, sbuf.st_size);
657                         return 0;
658                 }
659                 type = sqlite3_column_type(ifile, 1);
660                 if (type == SQLITE_NULL) {
661                         /* TODO assert, this shouldn't be possible? */
662                         fprintf(stderr, "no file data\n");
663                         sqlite3_finalize(ifile);
664                 munmap(content, sbuf.st_size);
665                         return 0;
666                         /* which is fine, just need to update the row then */
667                 }
668                 havedata = sqlite3_column_int(ifile, 1);
669         }
670
671         sqlite3_finalize(ifile);
672
673         if (!havedata) {
674                 /* compress */
675                 outbuf = compresslzma(content, sbuf.st_size, &outlen);
676                 if (!outbuf) {
677                         fprintf(stderr, "compresslzma failed\n");
678                         munmap(content, sbuf.st_size);
679                         return 0;
680                 }
681
682                 /* insert */
683                 if (haverow) {
684                         rc = sqlite3_prepare_v2(db, "update files set size = ?, content = ? where hash = ?", -1, &ifile,0);
685                 } else {
686                         rc = sqlite3_prepare_v2(db, "insert into files (size, content, hash) values (?,?,?)", -1, &ifile,0);
687                 }
688
689                 if (rc != SQLITE_OK) {
690                         SQLERROR(sqlite3_errmsg(db));
691                         fprintf(stderr, "cant prepare data\n");
692                         munmap(content, sbuf.st_size);
693                         return 0;
694                 }
695
696                 rc = sqlite3_bind_int64(ifile, 1, (sqlite3_int64)sbuf.st_size);
697                 if (rc != SQLITE_OK) {
698                         SQLERROR(sqlite3_errmsg(db));
699                         sqlite3_finalize(ifile);
700                         fprintf(stderr, "cant bind size\n");
701                         munmap(content, sbuf.st_size);
702                         return 0;
703                 }
704
705                 rc = sqlite3_bind_blob64(ifile, 2, outbuf,
706                                 (sqlite3_int64)outlen, SQLITE_STATIC);
707                 if (rc != SQLITE_OK) {
708                         SQLERROR(sqlite3_errmsg(db));
709                         sqlite3_finalize(ifile);
710                         fprintf(stderr, "cant bind content\n");
711                         munmap(content, sbuf.st_size);
712                         return 0;
713                 }
714
715                 rc = sqlite3_bind_text(ifile, 3, hash, 64, SQLITE_STATIC);
716                 if (rc != SQLITE_OK) {
717                         SQLERROR(sqlite3_errmsg(db));
718                         fprintf(stderr, "cant bind hash\n");
719                         sqlite3_finalize(ifile);
720                         munmap(content, sbuf.st_size);
721                         return 0;
722                 }
723
724                 rc = sqlite3_step(ifile);
725                 if (rc != SQLITE_DONE) {
726                         SQLERROR(sqlite3_errmsg(db));
727                         sqlite3_finalize(ifile);
728                         munmap(content, sbuf.st_size);
729                         return 0;
730                 }
731                 sqlite3_finalize(ifile);
732
733                 /* don't need the original file now */
734         }
735
736         if (!set_elf_info(zpm, hash, content, sbuf.st_size)) {
737                 fprintf(stderr, "setting elf info failed\n");
738                 munmap(content, sbuf.st_size);
739                 return 0;
740         }
741
742         munmap(content, sbuf.st_size);
743
744         /* return */
745         return 1;
746 }