File: main.c

package info (click to toggle)
blackbox-terminal 0.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,288 kB
  • sloc: xml: 197; ansic: 53; makefile: 23; python: 12; sh: 11
file content (75 lines) | stat: -rw-r--r-- 1,601 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
#include <pwd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

/**
 * This is a simple program meant to be launched with flatpak-spawn --host to
 * retrieve host information Flatpak'ed apps don't have access to. The original
 * idea for this program came from
 * https://github.com/gnunn1/tilix/blob/master/experimental/flatpak/tilix-flatpak-toolbox.c
 */

int die(int code, char *fmt, ...) __attribute__((format(printf, 2, 0)));

int die(int code, char *fmt, ...)
{
  va_list list;
  va_start(list, fmt);
  vfprintf(stderr, fmt, list);
  return code;
}

int main(int argc, const char *argv[])
{
  if (argc < 2)
  {
    return die(1, "Not enough arguments.\n");
  }

  if (strcmp(argv[1], "tcgetpgrp") == 0)
  {
    // This is not needed
    if (argc != 3)
    {
      return die(1, "Missing argument `fd` for tcgetpgrp.\n");
    }

    // This is 3 because we create an array of fds that will be passed to this
    // program via a Flatpak DBus call, and the vte pty we need is in the 4th
    // slot in that array.
    pid_t pid = tcgetpgrp(3);

    printf("%d\n", pid);

    return 0;
  }
  else if (strcmp(argv[1], "stat") == 0)
  {
    if (argc < 3)
    {
      return die(1, "Missing argument 'pid' for getting euid.\n");
    }

    struct stat s;

    if (stat(argv[2], &s) == -1)
    {
      printf("STAT FAILED FOR PID %s\n", argv[2]);
      return -1;
    }

    printf("%d\n", s.st_uid);
    return 0;
  }
  else
  {
    return die(1, "Unknown command '%s'.\n", argv[1]);
  }

  return 0;
}