12,982
回編集
(ページの作成:「== 概要 == サービスとは、PCの起動時に自動的に実行され、バックグラウンドで仕事をするために待機するソフトウェアのことである。<br> <br> 一般的に、サービスはグラフィカルユーザインターフェースを持たず、ユーザの操作無しに動作する。<br> 最もよく知られているサービスは、Web、メール、データベース等のサーバで、Apache、MySQL等がある。<br…」) |
|||
58行目: | 58行目: | ||
== サンプルコード == | == サンプルコード == | ||
<code>QProcess</code> | <code>QProcess</code>クラスを使用せずに、Systemdを利用してSSHデーモンを起動する場合は、Systemdのライブラリを使用する必要がある。<br> | ||
<br> | <br> | ||
以下の例では、Systemdのライブラリを使用してSSHデーモンを起動している。<br> | 以下の例では、Systemdのライブラリを使用してSSHデーモンを起動している。<br> | ||
# <code> | # <code>sd_bus_open_system</code>関数を使用して、システムバスへの接続を開く。 | ||
#: | #: 戻り値が負の値の場合は、接続に失敗したことを示す。 | ||
# | # <code>sd_bus_call_method</code>関数を使用して、SystemdのD-Busインターフェイスを介してsshd.serviceを起動する。 | ||
#: 関数の引数には、バスの接続、D-Busサービス名、D-Busオブジェクトのパス名、インターフェイス名、メソッド名、入力引数、出力引数、起動するサービス名とモードを指定する。 | |||
#: 戻り値が負の値の場合は、サービスの起動に失敗したことを示す。 | |||
#: | #: サービスの起動に成功した場合は、成功メッセージをデバッグ出力に表示している。 | ||
#: | # <code>sd_bus_unref</code>関数を使用して、バスの接続を閉じる。 | ||
#: | |||
# | |||
<br> | <br> | ||
このサンプルコードを実行するには、Systemdライブラリがインストールされている必要がある。<br> | |||
また、適切な権限でプログラムを実行する必要がある。<br> | また、適切な権限でプログラムを実行する必要がある。<br> | ||
<br> | <br> | ||
80行目: | 78行目: | ||
#include <QCoreApplication> | #include <QCoreApplication> | ||
#include <QDebug> | #include <QDebug> | ||
#include <systemd/sd- | #include <systemd/sd-bus.h> | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
86行目: | 84行目: | ||
QCoreApplication a(argc, argv); | QCoreApplication a(argc, argv); | ||
// | // Systemdを使用してSSHデーモンを起動 | ||
int ret = | sd_bus *bus = nullptr; | ||
if (ret = | int ret = sd_bus_open_system(&bus); | ||
qDebug() << " | if (ret < 0) { | ||
qDebug() << QString("システムD-Busの接続に失敗: %1").arg(strerror(-ret)); | |||
return -1; | |||
} | |||
ret = sd_bus_call_method(bus, | |||
"org.freedesktop.systemd1", | |||
"/org/freedesktop/systemd1", | |||
"org.freedesktop.systemd1.Manager", | |||
"StartUnit", | |||
nullptr, | |||
nullptr, | |||
"ss", | |||
"sshd.service", | |||
"replace"); | |||
if (ret < 0) { | |||
qDebug() << QString("SSHサービスの開始に失敗: %1").arg(strerror(-ret)); | |||
sd_bus_unref(bus); | |||
return -1; | return -1; | ||
} | } | ||
qDebug() << "SSHサービスの開始に成功"; | |||
sd_bus_unref(bus); | |||
return a.exec(); | return a.exec(); |