summaryrefslogtreecommitdiff
path: root/src/firestart
blob: 5cc1c92995c50893356c39f26e1459d72ea6917c (plain)
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
#!/bin/bash

UNIT=firefox@$DISPLAY

if systemctl --user is-active "$UNIT" >/dev/null
then
    echo "Attemping to contact existing Firefox instance." >&2
    # Ask the existing firefox to open a new window.

    # Firefox does not include an official API to do this in a way that
    # is not racy.  If we merely do this:
    #
    #     exec firefox "$@"
    #
    # ...then the unit could become inactive after our check, in which
    # case this launches a new firefox outside the container!
    #
    # So instead, we launch the same command using a systemd container
    # that prevents Firefox from forking off a new process.
    #
    # Well, actually, we let it fork off _one_ process, since otherwise
    # it segfaults.  But this _does_ happen to prevent it starting a new
    # firefox instance!  Firefox safely segfaults if it is limited this
    # way while trying to start a new instance.
    #
    # A more "correct" approach is available here:
    # https://github.com/ayosec/findfox
    exec systemd-run \
      --user \
      --property Environment="DISPLAY=$DISPLAY" \
      --property Environment=XAUTHORITY="$XAUTHORITY" \
      --property TasksMax=2 \
      --wait \
      --pipe \
      -q \
      -- /bin/sh -c 'exec firefox "$@"' sh "$@"
else
    systemctl --user reset-failed "$UNIT" 2>/dev/null
    systemctl --user start "$UNIT"
fi