Skip to content

Commit

Permalink
Revert "fix setting preference rollback error"
Browse files Browse the repository at this point in the history
This reverts commit 963e4e9.
  • Loading branch information
maurizio-lombardi committed May 29, 2023
1 parent 794e392 commit 974c2df
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions scripts/targetcli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ from __future__ import print_function
from os import getuid, getenv
from targetcli import UIRoot
from rtslib_fb import RTSLibError
from configshell_fb import Console, ConfigShell, ExecutionError
from configshell_fb import ConfigShell, ExecutionError
from targetcli import __version__ as targetcli_version

import sys
Expand Down Expand Up @@ -88,27 +88,30 @@ def usage_version(cmd):
if cmd in ("version", "--version", "-v"):
version()

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

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

Expand Down Expand Up @@ -248,23 +251,21 @@ def main():
'''
Start the targetcli shell.
'''
console = Console()
shell = TargetCLI(getenv("TARGETCLI_HOME", '~/.targetcli'))

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

try:
lkfd = open(lock_file, 'w+')
lkfd = open(lock_file, 'w+');
except IOError as e:
text = "opening lockfile failed: %s" %str(e)
console.display(console.render_text(text, 'red'))
shell.con.display(
shell.con.render_text("opening lockfile failed: %s" %str(e),
'red'))
sys.exit(1)

try_op_lock(console, lkfd)

targetcli_dir = getenv("TARGETCLI_HOME", '~/.targetcli')
shell = TargetCLI(targetcli_dir, console)
try_op_lock(shell, lkfd)

use_daemon = False
if shell.prefs['auto_use_daemon']:
Expand All @@ -291,7 +292,6 @@ def main():
shell.con.display(shell.con.render_text(str(error), 'red'))
if not is_root:
shell.con.display(shell.con.render_text("Retry as root.", 'red'))
release_op_lock(console, lkfd)
sys.exit(-1)

if len(sys.argv) > 1:
Expand All @@ -302,9 +302,7 @@ def main():
shell.run_cmdline(" ".join(sys.argv[1:]))
except Exception as e:
print(str(e), file=sys.stderr)
release_op_lock(console, lkfd)
sys.exit(1)
release_op_lock(console, lkfd)
sys.exit(0)

shell.con.display("targetcli shell version %s\n"
Expand All @@ -324,7 +322,7 @@ def main():
shell.log.info("Global pref auto_save_on_exit=true")
root_node.ui_command_saveconfig()

release_op_lock(console, lkfd)
release_op_lock(shell, lkfd)


if __name__ == "__main__":
Expand Down

0 comments on commit 974c2df

Please sign in to comment.