summaryrefslogtreecommitdiff
path: root/session-cmd/session-cmd-common.sh
blob: 41f606947c46b4303ca009dc936280de0adbbef1 (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
41
42
43
44
45
46
47
48
49
50
51
52
# -------
# File:        session-cmd-common.sh
# Description: common functions for session-cmd scripts
# Author:      Luis Garcia Gisbert <garcia_luigis@gva.es>
# Copyright (C) Luis Garcia Gisbert <garcia_luigis@gva.es>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA
# --------

UNIX_SOCKET_DIR="/tmp/session-cmd"

socket_basename(){
   echo "$(id -un)"
}

create_socket(){
   local i MAX_TRY UNIX_SOCKET
   i=0
   MAX_TRY=99
   while [ $i -lt $MAX_TRY ] ; do
      UNIX_SOCKET="${UNIX_SOCKET_DIR}-$(socket_basename)/$(socket_basename)-${i}"
      [ -S "${UNIX_SOCKET}.sock" ] || break
      if [ -r "${UNIX_SOCKET}.pid" ] ; then
         kill -9 "$(cat "${UNIX_SOCKET}.pid")" >/dev/null 2>/dev/null || true
         break
      fi
      i=$(($i + 1))
   done
   [ $i -lt $MAX_TRY ] || return 1
   mkdir -p "${UNIX_SOCKET_DIR}-$(socket_basename)"
   [ -d "${UNIX_SOCKET_DIR}-$(socket_basename)" ] || return 1
   echo "$UNIX_SOCKET"
   return 0
}

list_sockets(){
   [ -d "${UNIX_SOCKET_DIR}-$(socket_basename)" ] || return 0
   find "${UNIX_SOCKET_DIR}-$(socket_basename)" -type s -name "$(socket_basename)-*.sock" 2>/dev/null
   return 0
}