summaryrefslogtreecommitdiffstats
path: root/smartd.cygwin.initd.in
blob: 8879b749c61a504dad9fac605dc2e51c392538aa (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#! /bin/sh
#
# smartmontools initd file for Cygwin smartd
#
# Home page of code is: http://www.smartmontools.org
#
# Copyright (C) 2004-17 Christian Franke
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# $Id: smartd.cygwin.initd.in 4760 2018-08-19 18:45:53Z chrfranke $
#

SMARTD_BIN=/usr/local/sbin/smartd

# The following settings may be changed by the configuration file below
# Service Name (must be unique)
smartd_svcname=smartd
# Service display name
smartd_svcdisp="CYGWIN smartd"
# Service description
smartd_svcdesc="\
Controls and monitors storage devices using the Self-Monitoring, \
Analysis and Reporting Technology System (SMART) built into \
ATA/SATA and SCSI/SAS hard drives and solid-state drives. \
www.smartmontools.org"

# Source configuration file.
[ -r /etc/sysconfig/smartmontools ] && . /etc/sysconfig/smartmontools

PID_FILE=/var/run/smartd.pid
RETVAL=0

# Note: "[ -r $PID_FILE ]" is not used here. On Cygwin, this command may
# return success even if the file is present but cannot be read by current user.
# If smartd is running as service, smartd.pid is owned by local system account
# which is different from any user ever executing this script.

case "$1" in
  start)
    if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then
      echo -n "Starting service $smartd_svcname: "
      cygrunsrv -S "$smartd_svcname"
    else
      echo -n "Starting smartd as daemon: "
      $SMARTD_BIN -p $PID_FILE $smartd_opts
    fi
    RETVAL=$?
    ;;
  stop)
    echo -n "Shutting down smartd: "
    pid="`cat $PID_FILE 2>/dev/null`" && kill "$pid"
    RETVAL=$?
    ;;
  reload)
    echo -n "Reloading smartd configuration: "
    pid="`cat $PID_FILE 2>/dev/null`" && kill -HUP "$pid"
    RETVAL=$?
    ;;
  report)
    echo -n "Checking SMART devices now: "
    pid="`cat $PID_FILE 2>/dev/null`" && kill -USR1 "$pid"
    RETVAL=$?
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    exit $?
    ;;
  install)
    shift
    [ $# -eq 0 ] || smartd_opts="$*"
    dep=; dep2=
    if cygrunsrv -L 2>/dev/null | grep "^syslogd$" >/dev/null 2>&1; then
      dep="syslogd"
    fi
    if cygrunsrv -L 2>/dev/null | grep "^syslog-ng" >/dev/null 2>&1; then
      dep2="syslog-ng"
    fi
    if [ -z "$dep" ]; then
      if [ -z "$dep2" ]; then
        echo "Warning: no syslog service installed, smartd will write to windows event log.";
      else
        dep="$dep2"
      fi
    else
      if [ -z "$dep2" ]; then
        :
      else
        dep=
        echo "Warning: both syslogd and syslog-ng installed, dependency not set."
      fi
    fi
    echo "Installing service ${smartd_svcname}${dep:+ (depending on '$dep')}${smartd_opts:+ with options '$smartd_opts'}:"
    cygrunsrv -I "$smartd_svcname" -d "$smartd_svcdisp"  -f "$smartd_svcdesc" ${dep:+-y} $dep \
              -e CYGWIN="$CYGWIN" -p $SMARTD_BIN -a "-n -p ${PID_FILE}${smartd_opts:+ }$smartd_opts"
    RETVAL=$?
    ;;
  remove)
    echo "Removing service $smartd_svcname:"
    cygrunsrv -R "$smartd_svcname"
    RETVAL=$?
    ;;
  status)
    echo -n "Checking smartd status: "
    if cygrunsrv -L 2>/dev/null | grep "^${smartd_svcname}$" >/dev/null 2>&1; then
      if cygrunsrv -Q "$smartd_svcname" 2>/dev/null | grep "State *: Running" >/dev/null 2>&1; then
        echo "running as service '$smartd_svcname'."
      elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then
        echo "installed as service '$smartd_svcname' but running as daemon."
      else
        echo "installed as service '$smartd_svcname' but not running."
        RETVAL=1
      fi
    elif ps -e 2>/dev/null | grep " ${SMARTD_BIN}$" >/dev/null 2>&1; then
      echo "running as daemon."
    else
      echo "not running."
      RETVAL=1
    fi
    exit $RETVAL
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload|report|status}"
    echo "       $0 {install [options]|remove}"
    exit 1
esac

if [ "$RETVAL" -eq 0 ]; then echo "done"; else echo "ERROR"; fi
exit $RETVAL