Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

targetclid: add daemonize component for targetcli #132

Merged
merged 3 commits into from
Jun 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 299 additions & 0 deletions daemon/targetclid
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
#!/usr/bin/python

'''
targetclid

This file is part of targetcli-fb.
Copyright (c) 2019 by Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
'''

from __future__ import print_function
from targetcli import UIRoot
from targetcli import __version__ as targetcli_version
from configshell_fb import ConfigShell
from os import getuid, getenv, unlink
from threading import Thread

import sys
import socket
import struct
import fcntl
import signal


err = sys.stderr

class TargetCLI:
def __init__(self):
'''
initializer
'''
# socket for unix communication
self.socket_path = '/var/run/targetclid.sock'
# pid file for defending on multiple daemon runs
self.pid_file = '/var/run/targetclid.pid'
# lockfile for serializing multiple client requests
self.lock_file = '/var/run/targetclid.lock'

self.NoSignal = True

# shell console methods
self.shell = ConfigShell(getenv("TARGETCLI_HOME", '~/.targetcli'))
self.con = self.shell.con
self.display = self.shell.con.display
self.render = self.shell.con.render_text

# Handle SIGINT SIGTERM SIGHUP gracefully
signal.signal(signal.SIGINT, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
signal.signal(signal.SIGHUP, self.signal_handler)

try:
self.pfd = open(self.pid_file, 'w+');
except IOError as e:
self.display(
self.render(
"opening pidfile failed: %s" %str(e),
'red'))
sys.exit(1)

self.try_pidfile_lock()

is_root = False
if getuid() == 0:
is_root = True

try:
root_node = UIRoot(self.shell, as_root=is_root)
root_node.refresh()
except Exception as error:
self.display(self.render(str(error), 'red'))
if not is_root:
self.display(self.render("Retry as root.", 'red'))
self.pfd.close()
sys.exit(1)

try:
self.lkfd = open(self.lock_file, 'w+');
except IOError as e:
self.display(
self.render(
"opening lockfile failed: %s" %str(e),
'red'))
self.pfd.close()
sys.exit(1)

# Keep track, for later use
self.con_stdout_ = self.con._stdout
self.con_stderr_ = self.con._stderr


def __del__(self):
'''
destructor
'''
if not self.lkfd.closed:
self.lkfd.close()

if not self.pfd.closed:
self.pfd.close()


def signal_handler(self, signum, frame):
'''
signal handler
'''
self.NoSignal = False


def try_pidfile_lock(self):
'''
get lock on pidfile, which is to check if targetclid is running
'''
# check if targetclid is already running
lock = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
try:
fcntl.fcntl(self.pfd, fcntl.F_SETLK, lock)
except Exception:
self.display(self.render("targetclid is already running...", 'red'))
self.pfd.close()
sys.exit(1)


def release_pidfile_lock(self):
'''
release lock on pidfile
'''
lock = struct.pack('hhllhh', fcntl.F_UNLCK, 0, 0, 0, 0, 0)
try:
fcntl.fcntl(self.pfd, fcntl.F_SETLK, lock)
except Exception, e:
self.display(
self.render(
"fcntl(UNLCK) on pidfile failed: %s" %str(e),
'red'))
self.pfd.close()
sys.exit(1)
self.pfd.close()


def try_op_lock(self):
'''
acquire a blocking lock on lockfile, to serialize multiple client requests
'''
try:
fcntl.flock(self.lkfd, fcntl.LOCK_EX) # wait here until ongoing request is finished
except Exception, e:
self.display(
self.render(
"taking lock on lockfile failed: %s" %str(e),
'red'))
sys.exit(1)


def release_op_lock(self):
'''
release blocking lock on lockfile, which can allow other requests process
'''
try:
fcntl.flock(self.lkfd, fcntl.LOCK_UN) # allow other requests now
except Exception, e:
self.display(
self.render(
"unlock on lockfile failed: %s" %str(e),
'red'))
sys.exit(1)


def client_thread(self, connection):
'''
Handle commands from client
'''
self.try_op_lock()

still_listen = True
# Receive the data in small chunks and retransmit it
while still_listen:
data = connection.recv(65535)
if "-END@OF@DATA-" in data:
connection.close()
still_listen = False
else:
self.con._stdout = self.con._stderr = f = open("/tmp/data.txt", "w")
try:
# extract multiple commands delimited with '%'
list_data = data.split('%')
for cmd in list_data:
self.shell.run_cmdline(cmd)
except Exception as e:
print(str(e), file=f) # push error to stream

# Restore
self.con._stdout = self.con_stdout_
self.con._stderr = self.con_stderr_
f.close()

with open('/tmp/data.txt', 'r') as f:
output = f.read()

var = struct.pack('i', len(output))
connection.sendall(var) # length of string
connection.sendall(output) # actual string

self.release_op_lock()


def usage():
print("Usage: %s [--version|--help]" % sys.argv[0], file=err)
print(" --version\t\tPrint version", file=err)
print(" --help\t\tPrint this information", file=err)
sys.exit(0)


def version():
print("%s version %s" % (sys.argv[0], targetcli_version), file=err)
sys.exit(0)


def usage_version(cmd):
if cmd in ("help", "--help", "-h"):
usage()

if cmd in ("version", "--version", "-v"):
version()


def main():
'''
start targetclid
'''
if len(sys.argv) > 1:
usage_version(sys.argv[1])
print("unrecognized option: %s" % (sys.argv[1]))
sys.exit(-1)

to = TargetCLI()

# Make sure file doesn't exist already
try:
unlink(to.socket_path)
except:
pass

# Create a TCP/IP socket
try:
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
except socket.error as err:
to.display(to.render(err, 'red'))
sys.exit(1)

# Bind the socket path
try:
sock.bind(to.socket_path)
except socket.error as err:
to.display(to.render(err, 'red'))
sys.exit(1)

# Listen for incoming connections
try:
sock.listen(1)
except socket.error as err:
to.display(to.render(err, 'red'))
sys.exit(1)

while to.NoSignal:
try:
# Wait for a connection
connection, client_address = sock.accept()
except socket.error as err:
to.display(to.render(err, 'red'))
break;

thread = Thread(target=to.client_thread, args=(connection,))
thread.start()
try:
thread.join()
except:
to.display(to.render(str(error), 'red'))

to.release_pidfile_lock()

if not to.NoSignal:
to.display(to.render("Signal received, quiting gracefully!", 'green'))
sys.exit(1)


if __name__ == "__main__":
main()
Loading