list-system-services.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #! /usr/bin/env lua
  2. local help = [[
  3. Usage: lua list-system-services.lua [--session|--system]
  4. List services on the system bus (default) or the session bus.
  5. ]]
  6. --[[
  7. This example is the rewrite of list-system-services.py in Lua.
  8. Copyleft (C) 2012 Ildar Mulyukov
  9. Original copyright follows:
  10. # Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
  11. # Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
  12. #
  13. # Permission is hereby granted, free of charge, to any person
  14. # obtaining a copy of this software and associated documentation
  15. # files (the "Software"), to deal in the Software without
  16. # restriction, including without limitation the rights to use, copy,
  17. # modify, merge, publish, distribute, sublicense, and/or sell copies
  18. # of the Software, and to permit persons to whom the Software is
  19. # furnished to do so, subject to the following conditions:
  20. #
  21. # The above copyright notice and this permission notice shall be
  22. # included in all copies or substantial portions of the Software.
  23. #
  24. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  28. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  29. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  31. # DEALINGS IN THE SOFTWARE.
  32. ]]
  33. local lgi = require 'lgi'
  34. local Gio = lgi.require 'Gio'
  35. -- main(argv):
  36. local argv = {...}
  37. local factory = function()
  38. return Gio.bus_get_sync(Gio.BusType.SYSTEM)
  39. end
  40. if #argv > 1 then
  41. print(help)
  42. return -1
  43. else
  44. if #argv == 1 then
  45. if argv[1] == '--session' then
  46. factory = function()
  47. return Gio.bus_get_sync(Gio.BusType.SESSION)
  48. end
  49. else
  50. if argv[1] ~= '--system' then
  51. print(help)
  52. return -1
  53. end
  54. end
  55. end
  56. end
  57. -- Get a connection to the system or session bus as appropriate
  58. -- We're only using blocking calls, so don't actually need a main loop here
  59. local bus = factory()
  60. -- This could be done by calling bus.list_names(), but here's
  61. -- more or less what that means:
  62. -- Get a reference to the desktop bus' standard object, denoted
  63. -- by the path /org/freedesktop/DBus.
  64. -- dbus_object = bus.get_object('org.freedesktop.DBus',
  65. -- '/org/freedesktop/DBus')
  66. -- The object /org/freedesktop/DBus
  67. -- implements the 'org.freedesktop.DBus' interface
  68. -- dbus_iface = dbus.Interface(dbus_object, 'org.freedesktop.DBus')
  69. -- One of the member functions in the org.freedesktop.DBus interface
  70. -- is ListNames(), which provides a list of all the other services
  71. -- registered on this bus. Call it, and print the list.
  72. -- services = dbus_iface.ListNames()
  73. local var, err = bus:call_sync('org.freedesktop.DBus',
  74. '/org/freedesktop/DBus',
  75. 'org.freedesktop.DBus',
  76. 'ListNames',
  77. nil,
  78. nil,
  79. Gio.DBusConnectionFlags.NONE,
  80. -1)
  81. -- We know that ListNames returns '(as)'
  82. local services = var[1].value
  83. -- services.sort() - is incorrect as GVariant is immutable
  84. for i = 1, #services do
  85. print (services[i])
  86. end