graysky2 / profile-sync-daemon

Symlinks and syncs browser profile dirs to RAM thus reducing HDD/SDD calls and speeding-up browsers.
https://wiki.archlinux.org/index.php/Profile-sync-daemon
Other
899 stars 84 forks source link

Running Firefox 94.0 not detected at startup #310

Open rstech209 opened 2 years ago

rstech209 commented 2 years ago

First of all, thanks for this software.

With Firefox 94.0 (and since version 93.0?) pgrep and pkill are not executed in the profile-sync-daemon script. (pgrep error code = 1)

Functions concerned:

running_check ()
kill_browsers ()

pgrep takes into account the first 15 characters of /proc/<firefox_pid>/stat which is: GeckoMain See Note in man pgrep/pkill:

"Note: The process name used for matching is limited to the 15 characters present in the output of /proc/pid/stat.
Use the -f option to match against the complete command line, /proc/pid/cmdline. "

Using the -f option, the detection of a firefox process is OK

This is probably not a good solution, but it's OK after replacing in the main script /usr/bin/profile-sync-daemon: pgrep/pkill -x -u "$user" "$PSNAME" by pgrep/pkill -u "$user" -f "$PSNAME"

!!!Warning!!! : in this case all the processes and sub-processes containing the word 'firefox' in their command line will be taken into account for detection (running_check () ) and stopped (kill_browsers ()).

graysky2 commented 2 years ago

Thanks for reporting this... will need to look into a workaround if the one you proposed is problematic... need to think though the full pattern match.

rstech209 commented 2 years ago

I think I have found a solution that meets the following constraints:

I created a new function:

running_browser_pid() {
  local pid_list geckomain_pid

  PSNAME_PID="$(pgrep -x -u "$user" "$PSNAME")"
  # needed for browser using a process name different than application name
  # e.g. GeckoMain for firefox or librewolf
  if [[ -z "$PSNAME_PID" ]]; then
    pid_list="$(pgrep -u "$user" -f "$PSNAME")"
    if [[ -n "$pid_list" ]]; then
      geckomain_pid="$(ps h -C GeckoMain -o pid:1)"
      if [[ -n "$geckomain_pid" ]]; then
        PSNAME_PID="$( grep "$geckomain_pid" <<< "$pid_list" )"
      fi
    fi
  fi
}

Other functions running_check () and kill_browsers () become

running_check() {
  # check for browsers running and refuse to start if so
  # without this cannot guarantee profile integrity
  local browser
  for browser in "${BROWSERS[@]}"; do
    load_env_for "$browser"
    [[ -z "$PSNAME" ]] && continue
    running_browser_pid
    if [[ -n "$PSNAME_PID" ]]; then
      echo "Refusing to start; $browser is running by $user!"
      exit 1
    fi
  done
}
kill_browsers() {
  # check for browsers running and kill them to safely sync/unsync
  # without this cannot guarantee profile integrity
  local browser
  for browser in "${BROWSERS[@]}"; do
    load_env_for "$browser"

    local x=1
    while [[ $x -le 60 ]]; do
      [[ -n "$PSNAME" ]] || break
      running_browser_pid
      [[ -n "$PSNAME_PID" ]] || break

      if [[ $x -le 5 ]]; then
        kill -SIGTERM "${PSNAME_PID//$'\n'/' '}"
      else
        kill -SIGKILL "${PSNAME_PID//$'\n'/' '}"
      fi

      x=$(( x + 1 ))
      sleep .05
    done
  done
}
rstech209 commented 2 years ago

Accidentally closed

rstech209 commented 2 years ago

PR #316 fix the problem