Systemd开发之示例:EnableUnitFiles/DisableUnitFiles

#include <gio/gio.h>

#ifndef SSHD_SERVICE
#define SSHD_SERVICE "sshd.service"
#endif

static const gchar *service_list[] = { SSHD_SERVICE, NULL };

static gint
enable_ssh_service ()
{
  g_autoptr(GDBusConnection) connection = NULL;
  g_autoptr(GError) error = NULL;
  g_autoptr(GVariant) start_result = NULL;
  g_autoptr(GVariant) enable_result = NULL;

  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
  if (!connection)
    {
      g_critical ("Error connecting to D-Bus system bus: %s", error->message);
      return 1;
    }

  start_result = g_dbus_connection_call_sync (connection,
                                              "org.freedesktop.systemd1",
                                              "/org/freedesktop/systemd1",
                                              "org.freedesktop.systemd1.Manager",
                                              "StartUnit",
                                              g_variant_new ("(ss)",
                                                             SSHD_SERVICE,
                                                             "replace"),
                                              (GVariantType *) "(o)",
                                              G_DBUS_CALL_FLAGS_NONE,
                                              -1,
                                              NULL,
                                              &error);

  if (!start_result)
    {
      g_critical ("Error starting " SSHD_SERVICE ": %s", error->message);
      return 1;
    }

  enable_result = g_dbus_connection_call_sync (connection,
                                               "org.freedesktop.systemd1",
                                               "/org/freedesktop/systemd1",
                                               "org.freedesktop.systemd1.Manager",
                                               "EnableUnitFiles",
                                               g_variant_new ("(^asbb)",
                                                              service_list,
                                                              FALSE, FALSE),
                                               (GVariantType *) "(ba(sss))",
                                               G_DBUS_CALL_FLAGS_NONE,
                                               -1,
                                               NULL,
                                               &error);

  if (!enable_result)
    {
      g_critical ("Error enabling " SSHD_SERVICE ": %s", error->message);
      return 1;
    }

  return 0;
}

static gint
disable_ssh_service ()
{
  g_autoptr(GDBusConnection) connection = NULL;
  g_autoptr(GError) error = NULL;
  g_autoptr(GVariant) stop_result = NULL;
  g_autoptr(GVariant) disable_result = NULL;

  connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
  if (!connection)
    {
      g_critical ("Error connecting to D-Bus system bus: %s", error->message);
      return 1;
    }

  stop_result = g_dbus_connection_call_sync (connection,
                                             "org.freedesktop.systemd1",
                                             "/org/freedesktop/systemd1",
                                             "org.freedesktop.systemd1.Manager",
                                             "StopUnit",
                                             g_variant_new ("(ss)", SSHD_SERVICE, "replace"),
                                             (GVariantType *) "(o)",
                                             G_DBUS_CALL_FLAGS_NONE,
                                             -1,
                                             NULL,
                                             &error);
  if (!stop_result)
    {
      g_critical ("Error stopping " SSHD_SERVICE ": %s", error->message);
      return 1;
    }

  disable_result = g_dbus_connection_call_sync (connection,
                                                "org.freedesktop.systemd1",
                                                "/org/freedesktop/systemd1",
                                                "org.freedesktop.systemd1.Manager",
                                                "DisableUnitFiles",
                                                g_variant_new ("(^asb)", service_list, FALSE,
                                                               FALSE),
                                                (GVariantType *) "(a(sss))",
                                                G_DBUS_CALL_FLAGS_NONE,
                                                -1,
                                                NULL,
                                                &error);

  if (!stop_result)
    {
      g_critical ("Error disabling " SSHD_SERVICE ": %s", error->message);
      return 1;
    }

  return 0;
}

int
main (int    argc,
      char **argv)
{
  if (argc < 2)
    return 1;

  if (argv[1] == NULL)
    return 1;

  if (g_str_equal (argv[1], "enable"))
    return enable_ssh_service ();
  else if (g_str_equal (argv[1], "disable"))
    return disable_ssh_service ();

  return 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
7月 11 10:15:09 zhang-virtual-machine systemd[1]: Starting MySQL Community Server... zhang@zhang-virtual-machine:~$ journalctl -xeu mysql.service ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ An ExecStart= process belonging to unit mysql.service has exited. ░░ ░░ The process' exit code is 'exited' and its exit status is 1. 7月 11 10:16:07 zhang-virtual-machine systemd[1]: mysql.service: Failed with result 'exit-code'. ░░ Subject: Unit failed ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ The unit mysql.service has entered the 'failed' state with result 'exit-code'. 7月 11 10:16:07 zhang-virtual-machine systemd[1]: Failed to start MySQL Community Server. ░░ Subject: A start job for unit mysql.service has failed ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A start job for unit mysql.service has finished with a failure. ░░ ░░ The job identifier is 11397 and the job result is failed. 7月 11 10:16:07 zhang-virtual-machine systemd[1]: mysql.service: Scheduled restart job, restart counter is at 48. ░░ Subject: Automatic restarting of a unit has been scheduled ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ Automatic restarting of the unit mysql.service has been scheduled, as the result for ░░ the configured Restart= setting for the unit. 7月 11 10:16:07 zhang-virtual-machine systemd[1]: Stopped MySQL Community Server. ░░ Subject: A stop job for unit mysql.service has finished ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A stop job for unit mysql.service has finished. ░░ ░░ The job identifier is 11470 and the job result is done. 7月 11 10:16:07 zhang-virtual-machine systemd[1]: Starting MySQL Community Server... ░░ Subject: A start job for unit mysql.service has begun execution ░░ Defined-By: systemd ░░ Support: http://www.ubuntu.com/support ░░ ░░ A start job for unit mysql.service has begun execution. ░░ ░░ The job identifier is 11470. lines 2126-2168/2168 (END)
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值