Fix the way root user is being determined in Python; Running scapy_daemon_server... 22/5322/1
authorYaroslav Brustinov <[email protected]>
Tue, 20 Dec 2016 15:47:22 +0000 (17:47 +0200)
committerYaroslav Brustinov <[email protected]>
Tue, 20 Dec 2016 15:47:22 +0000 (17:47 +0200)
Change-Id: Id70be83956a9b8279197c68dd58b674e972fc1a9
Signed-off-by: Yaroslav Brustinov <[email protected]>
scripts/dpdk_nic_bind.py
scripts/dpdk_setup_ports.py
scripts/master_daemon.py
scripts/scapy_daemon_server
scripts/trex_daemon_server

index e797666..6dec60c 100755 (executable)
@@ -41,7 +41,6 @@ import texttable
 sys.path.remove(text_tables_path)
 import re
 import termios
-import getpass
 
 # The PCI device class for ETHERNET devices
 ETHERNET_CLASS = "0200"
@@ -729,7 +728,7 @@ def main():
     do_arg_actions()
 
 if __name__ == "__main__":
-    if getpass.getuser() != 'root':
+    if os.getuid() != 0:
         print('Please run this program as root/with sudo')
         exit(1)
     main()
index fce099b..1edf8f8 100755 (executable)
@@ -13,7 +13,6 @@ import shlex
 import traceback
 from collections import defaultdict, OrderedDict
 from distutils.util import strtobool
-import getpass
 import subprocess
 import platform
 
@@ -984,7 +983,7 @@ To see more detailed info on interfaces (table):
 
 def main ():
     try:
-        if getpass.getuser() != 'root':
+        if os.getuid() != 0:
             raise DpdkSetup('Please run this program as root/with sudo')
 
         process_options ()
index 855358f..bcafe8e 100755 (executable)
@@ -1,7 +1,6 @@
 #!/usr/bin/python
 import os
 import sys
-import getpass
 import shutil
 import multiprocessing
 import logging
@@ -159,13 +158,13 @@ def _check_path_under_current_or_temp(path):
 
 ### Main ###
 
-if getpass.getuser() != 'root':
+if os.getuid() != 0:
     fail('Please run this program as root/with sudo')
 
 pid = os.getpid()
-ret = os.system('taskset -pc 0 %s' % pid)
+ret, out, err = run_command('taskset -pc 0 %s' % pid)
 if ret:
-    fail('Could not set self affinity to core zero.')
+    fail('Could not set self affinity to core zero. Result: %s' % [ret, out, err])
 
 daemon_actions = OrderedDict([('start', 'start the daemon'),
                               ('stop', 'exit the daemon process'),
@@ -208,7 +207,7 @@ args.trex_dir = os.path.abspath(args.trex_dir)
 args.daemon_type = args.daemon_type or 'master_daemon'
 
 stl_rpc_proxy_dir  = os.path.join(args.trex_dir, 'automation', 'trex_control_plane', 'stl', 'examples')
-stl_rpc_proxy      = SingletonDaemon('Stateless RPC proxy', 'trex_stl_rpc_proxy', args.stl_rpc_proxy_port, 'sudo -u nobody %s rpc_proxy_server.py' % sys.executable, stl_rpc_proxy_dir)
+stl_rpc_proxy      = SingletonDaemon('Stateless RPC proxy', 'trex_stl_rpc_proxy', args.stl_rpc_proxy_port, "su -s /bin/bash -c '%s rpc_proxy_server.py' nobody" % sys.executable, stl_rpc_proxy_dir)
 trex_daemon_server = SingletonDaemon('TRex daemon server', 'trex_daemon_server', args.trex_daemon_port, '%s trex_daemon_server start' % sys.executable, args.trex_dir)
 master_daemon      = SingletonDaemon('Master daemon', 'trex_master_daemon', args.master_port, start_master_daemon) # add ourself for easier check if running, kill etc.
 
index 490e803..99f8a6a 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-import os, sys, getpass
+import os, sys
 import tempfile
 import time
 import subprocess, shlex
@@ -11,7 +11,7 @@ def fail(msg):
     print(msg)
     sys.exit(-1)
 
-if getpass.getuser() != 'root':
+if os.getuid() != 0:
     fail('Please run this program as root/with sudo')
 
 pwd = os.path.abspath(os.path.dirname(__file__))
@@ -102,7 +102,7 @@ def start_daemon():
         return
     server_path = os.path.join(pwd, 'automation', 'trex_control_plane', 'stl', 'services', 'scapy_server')
     with tempfile.TemporaryFile() as stdout_file:
-        subprocess.Popen(shlex.split('sudo -u nobody %s scapy_zmq_server.py -s %s' % (sys.executable, args.daemon_port)), stdout = stdout_file,
+        subprocess.Popen(shlex.split("su -s /bin/bash -c '%s scapy_zmq_server.py -s %s' nobody" % (sys.executable, args.daemon_port)), stdout = stdout_file,
                     stderr = subprocess.STDOUT, cwd = server_path, close_fds = True, universal_newlines = True)
         ret = progress(is_running, 'Starting Scapy server', 'Scapy server is started', 'Scapy server failed to run')
         if ret:
@@ -151,8 +151,10 @@ if __name__ == '__main__':
         formatter_class = RawTextHelpFormatter,
     )
     
-    parser.add_argument('-p', '--port', type = int, default = 4507, metavar="PORT", dest="daemon_port", 
-            help="Select tcp port on which Scapy server listens.\nDefault port is 4507.", action="store")
+    parser.add_argument('-p', '--port', type = int, default = 4507, metavar = 'PORT', dest = 'daemon_port',
+            help='Select tcp port on which Scapy server will listen.\nDefault is 4507.')
+    #parser.add_argument('-c', '--core', type = int, default = 0,
+    #        help='Core number to set affinity.\nAdvised to set on free core or TRex master thread core\nDefault is 0.')
     parser.add_argument('action', choices=action_funcs.keys(),
                             action='store', help=actions_help)
     parser.usage = None
index 514dbd2..d7da283 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-import os, sys, getpass
+import os, sys
 import tempfile
 from time import time, sleep
 import subprocess, shlex, multiprocessing
@@ -11,7 +11,7 @@ def fail(msg):
     print(msg)
     sys.exit(-1)
 
-if getpass.getuser() != 'root':
+if os.getuid() != 0:
     fail('Please run this program as root/with sudo')
 
 sys.path.append(os.path.join('automation', 'trex_control_plane', 'server'))