samba-tool: Ensure that samba-tool processes --name=not-existing does not error
[Samba.git] / python / samba / netcmd / processes.py
blobc8000b7e49e53c11eeb9c76fb82601bdfde84513
1 # Unix SMB/CIFS implementation.
2 # List processes (to aid debugging on systems without setproctitle)
3 # Copyright (C) 2010-2011 Jelmer Vernooij <jelmer@samba.org>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Testbed for loadparm.c/params.c
20 # This module simply loads a specified configuration file and
21 # if successful, dumps it's contents to stdout. Note that the
22 # operation is performed with DEBUGLEVEL at 3.
24 # Useful for a quick 'syntax check' of a configuration file.
27 import os
28 import sys
30 import samba
31 import samba.getopt as options
32 from samba.netcmd import Command, CommandError, Option
33 from samba.messaging import Messaging
35 class cmd_processes(Command):
36 """List processes (to aid debugging on systems without setproctitle)."""
38 synopsis = "%prog [options]"
40 takes_optiongroups = {
41 "sambaopts": options.SambaOptions,
42 "versionopts": options.VersionOptions
45 takes_options = [
46 Option("--name", type=str,
47 help="Return only processes associated with one particular name"),
48 Option("--pid", type=int,
49 help="Return only names assoicated with one particular PID"),
52 takes_args = []
54 def run(self, sambaopts, versionopts, section_name=None,
55 name=None, pid=None):
57 lp = sambaopts.get_loadparm()
58 logger = self.get_logger("processes")
60 msg_ctx = Messaging()
62 if name is not None:
63 try:
64 ids = msg_ctx.irpc_servers_byname(name)
65 except KeyError:
66 ids = []
68 for server_id in ids:
69 self.outf.write("%d\n" % server_id.pid)
70 elif pid is not None:
71 names = msg_ctx.irpc_all_servers()
72 for name in names:
73 for server_id in name.ids:
74 if server_id.pid == int(pid):
75 self.outf.write("%s\n" % name.name)
76 else:
77 names = msg_ctx.irpc_all_servers()
78 self.outf.write(" Service: PID \n")
79 self.outf.write("-----------------------------\n")
80 for name in names:
81 for server_id in name.ids:
82 self.outf.write("%-16s %6d\n" % (name.name, server_id.pid))