Linux CIFS filesystem development
 help / color / mirror / Atom feed
From: Enzo Matsumiya <ematsumiya@suse.de>
To: linux-cifs@vger.kernel.org, linkinjeon@kernel.org
Cc: senozhatsky@chromium.org, sergey.senozhatsky@gmail.com,
	hyc.lee@gmail.com, smfrench@gmail.com,
	Enzo Matsumiya <ematsumiya@suse.de>
Subject: [PATCH 2/9] ksmbd-tools: move control functions to daemon
Date: Sun,  6 Mar 2022 22:33:37 -0300	[thread overview]
Message-ID: <20220307013344.29064-3-ematsumiya@suse.de> (raw)
In-Reply-To: <20220307013344.29064-1-ematsumiya@suse.de>

Move the control functionality to the daemon command.

This commit builds, but doesn't work since the commands will be
implemented in a next patch from this series.

Signed-off-by: Enzo Matsumiya <ematsumiya@suse.de>
---
 Makefile.am          |   2 +-
 configure.ac         |   1 -
 control/Makefile.am  |   7 ---
 control/control.c    | 128 -------------------------------------------
 daemon/daemon.c      |  85 +++++++++++++++++++++++++++-
 daemon/daemon.h      |  29 ++++++++++
 include/ksmbdtools.h |   1 +
 lib/ksmbdtools.c     |  24 ++++++++
 8 files changed, 139 insertions(+), 138 deletions(-)
 delete mode 100644 control/Makefile.am
 delete mode 100644 control/control.c
 create mode 100644 daemon/daemon.h

diff --git a/Makefile.am b/Makefile.am
index e3ee928691bf..b4e205895825 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,7 +2,7 @@
 
 ACLOCAL_AMFLAGS = -I m4
 
-SUBDIRS = lib daemon user share control
+SUBDIRS = lib daemon user share
 
 # other stuff
 EXTRA_DIST =			\
diff --git a/configure.ac b/configure.ac
index 1f107805325f..d7ec538cbbf0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -65,7 +65,6 @@ AC_CONFIG_FILES([
 	daemon/Makefile
 	user/Makefile
 	share/Makefile
-	control/Makefile
 ])
 
 AC_OUTPUT
diff --git a/control/Makefile.am b/control/Makefile.am
deleted file mode 100644
index 0b3559ab60ed..000000000000
--- a/control/Makefile.am
+++ /dev/null
@@ -1,7 +0,0 @@
-AM_CFLAGS = -I$(top_srcdir)/include $(GLIB_CFLAGS) $(LIBNL_CFLAGS) -fno-common
-LIBS = $(GLIB_LIBS)
-ksmbd_control_LDADD = $(top_builddir)/lib/libksmbdtools.a
-
-sbin_PROGRAMS = ksmbd.control
-
-ksmbd_control_SOURCES = control.c
diff --git a/control/control.c b/control/control.c
deleted file mode 100644
index 780a48ab9259..000000000000
--- a/control/control.c
+++ /dev/null
@@ -1,128 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-or-later
-/*
- *   Copyright (C) 2020 Samsung Electronics Co., Ltd.
- *
- *   linux-cifsd-devel@lists.sourceforge.net
- */
-
-#include <getopt.h>
-#include <fcntl.h>
-#include <errno.h>
-
-#include "ksmbdtools.h"
-#include "version.h"
-
-static void usage(void)
-{
-	fprintf(stderr, "Usage: ksmbd.control\n");
-	fprintf(stderr, "\t-s | --shutdown\n");
-	fprintf(stderr, "\t-d | --debug=all or [smb, auth, etc...]\n");
-	fprintf(stderr, "\t-c | --ksmbd-version\n");
-	fprintf(stderr, "\t-V | --version\n");
-
-	exit(EXIT_FAILURE);
-}
-
-static void show_version(void)
-{
-	printf("ksmbd-tools version : %s\n", KSMBD_TOOLS_VERSION);
-	exit(EXIT_FAILURE);
-}
-
-static int ksmbd_control_shutdown(void)
-{
-	int fd, ret;
-
-	terminate_ksmbd_daemon();
-
-	fd = open("/sys/class/ksmbd-control/kill_server", O_WRONLY);
-	if (fd < 0) {
-		pr_err("open failed: %d\n", errno);
-		return EXIT_FAILURE;
-	}
-
-	ret = write(fd, "hard", 4);
-	close(fd);
-	return ret != -1 ? EXIT_SUCCESS : EXIT_FAILURE;
-}
-
-static int ksmbd_control_show_version(void)
-{
-	int fd, ret;
-	char ver[255] = {0};
-
-	fd = open("/sys/module/ksmbd/version", O_RDONLY);
-	if (fd < 0) {
-		pr_err("open failed: %d\n", errno);
-		return EXIT_FAILURE;
-	}
-
-	ret = read(fd, ver, 255);
-	close(fd);
-	if (ret != -1)
-		pr_info("ksmbd version : %s\n", ver);
-	return ret != -1 ? EXIT_SUCCESS : EXIT_FAILURE;
-}
-
-static int ksmbd_control_debug(char *comp)
-{
-	int fd, ret;
-	char buf[255] = {0};
-
-	fd = open("/sys/class/ksmbd-control/debug", O_RDWR);
-	if (fd < 0) {
-		pr_err("open failed: %d\n", errno);
-		return EXIT_FAILURE;
-	}
-
-	ret = write(fd, comp, strlen(comp));
-	if (ret < 0)
-		goto out;
-	ret = read(fd, buf, 255);
-	if (ret < 0)
-		goto out;
-
-	pr_info("%s\n", buf);
-out:
-	close(fd);
-	return ret != -1 ? EXIT_SUCCESS : EXIT_FAILURE;
-}
-
-int main(int argc, char *argv[])
-{
-	int ret = EXIT_FAILURE;
-	int c;
-
-	set_logger_app_name("ksmbd.control");
-
-	if (getuid() != 0) {
-		pr_err("Please try it as root.\n");
-		return ret;
-	}
-
-	opterr = 0;
-	while ((c = getopt(argc, argv, "sd:cVh")) != EOF)
-		switch (c) {
-		case 's':
-			ret = ksmbd_control_shutdown();
-			break;
-		case 'd':
-			ret = ksmbd_control_debug(optarg);
-			break;
-		case 'c':
-			ret = ksmbd_control_show_version();
-			break;
-		case 'V':
-			show_version();
-			break;
-		case '?':
-		case 'h':
-		default:
-			usage();
-	}
-
-	if (argc < 2)
-		usage();
-
-	return ret;
-}
diff --git a/daemon/daemon.c b/daemon/daemon.c
index a3a683222a92..946f500bc977 100644
--- a/daemon/daemon.c
+++ b/daemon/daemon.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  *   Copyright (C) 2018 Samsung Electronics Co., Ltd.
+ *   Copyright (C) 2021 SUSE LLC
  *
  *   linux-cifsd-devel@lists.sourceforge.net
  */
@@ -13,6 +14,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <fcntl.h>
@@ -24,6 +26,7 @@
 #include "ipc.h"
 #include "rpc.h"
 #include "worker.h"
+#include "daemon.h"
 #include "config_parser.h"
 #include "management/user.h"
 #include "management/share.h"
@@ -555,12 +558,92 @@ static struct option opts[] = {
 	{NULL,		0,			NULL,	 0  }
 };
 
+int daemon_shutdown_cmd(void)
+{
+	int fd, ret;
+
+	if (get_running_pid() == -ENOENT) {
+		pr_info("Server is not running.\n");
+		exit(EXIT_FAILURE);
+	}
+
+	terminate_ksmbd_daemon();
+
+	fd = open(KSMBD_SYSFS_KILL_SERVER, O_WRONLY);
+	if (fd < 0) {
+		pr_debug("open failed (%d): %s\n", errno, strerr(errno));
+		return fd;
+	}
+
+	ret = write(fd, "hard", 4);
+	close(fd);
+	return ret;
+}
+
+int daemon_debug_cmd(char *debug_type)
+{
+	int i, fd, ret;
+	bool valid = false;
+	char buf[255] = { 0 };
+
+	for (i = 0; i < ARRAY_SIZE(debug_type_strings); i++) {
+		if (!strcmp(debug_type, debug_type_strings[i])) {
+			valid = true;
+			break;
+		}
+	}
+
+	if (!valid)
+		return -EINVAL;
+
+	ret = fd = open(KSMBD_SYSFS_DEBUG, O_RDWR);
+	if (fd < 0)
+		goto err_open;
+
+	ret = write(fd, debug_type, strlen(debug_type));
+	if (ret < 0)
+		goto err;
+
+	ret = read(fd, buf, 255);
+	if (ret < 0)
+		goto err;
+
+	pr_info("debug: %s\n", buf);
+err:
+	close(fd);
+err_open:
+	if (ret == -EBADF)
+		pr_debug("Can't open %s. Is ksmbd kernel module loaded?\n");
+	return ret;
+}
+
+int daemon_version_cmd(void)
+{
+	int fd, ret;
+	char version[255] = { 0 };
+
+	ret = fd = open(KSMBD_SYSFS_VERSION, O_RDONLY);
+	if (fd < 0)
+		goto err;
+
+	ret = read(fd, version, 255);
+	close(fd);
+
+err:
+	if (ret < 0)
+		pr_err("%s. Is kernel module loaded?\n", strerr(errno));
+	else
+		pr_info("ksmbd module version: %s\n", version);
+
+	return ret;
+}
+
 int main(int argc, char *argv[])
 {
 	int systemd_service = 0;
 	int c;
 
-	set_logger_app_name("ksmbd.mountd");
+	set_logger_app_name("ksmbd.daemon");
 	memset(&global_conf, 0x00, sizeof(struct smbconf_global));
 	global_conf.pwddb = PATH_PWDDB;
 	global_conf.smbconf = PATH_SMBCONF;
diff --git a/daemon/daemon.h b/daemon/daemon.h
new file mode 100644
index 000000000000..ca064b2b732d
--- /dev/null
+++ b/daemon/daemon.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ *   Copyright (C) 2021 SUSE LLC
+ *
+ *   linux-cifsd-devel@lists.sourceforge.net
+ */
+
+#ifndef __DAEMON_H__
+#define __DAEMON_H__
+
+#define KSMBD_SYSFS_KILL_SERVER "/sys/class/ksmbd-control/kill_server"
+#define KSMBD_SYSFS_DEBUG	"/sys/class/ksmbd-control/debug"
+#define KSMBD_SYSFS_VERSION	"/sys/module/ksmbd/version"
+
+static const char * const debug_type_strings[] = {
+	"all", "smb", "auth", "vfs", "oplock", "ipc", "conn", "rdma"
+};
+
+static struct option daemon_opts[] = {
+	{ "port", required_argument, NULL, 'p' },
+	{ "config", required_argument, NULL, 'c' },
+	{ "usersdb", required_argument, NULL, 'u' },
+	{ "nodetach", no_argument, NULL, 'n' },
+	{ "systemd", no_argument, NULL, 's' },
+	{ "help", no_argument, NULL, 'h' },
+	{ 0, 0, 0, 0 },
+};
+
+#endif /* __DAEMON_H__ */
diff --git a/include/ksmbdtools.h b/include/ksmbdtools.h
index c51673e0253f..170ce23ead2c 100644
--- a/include/ksmbdtools.h
+++ b/include/ksmbdtools.h
@@ -168,6 +168,7 @@ enum charset_idx {
 
 extern char *ksmbd_conv_charsets[KSMBD_CHARSET_MAX + 1];
 
+int get_running_pid(void);
 void notify_ksmbd_daemon(void);
 void terminate_ksmbd_daemon(void);
 int test_file_access(char *conf);
diff --git a/lib/ksmbdtools.c b/lib/ksmbdtools.c
index 91d82946f6d6..b636f34af98e 100644
--- a/lib/ksmbdtools.c
+++ b/lib/ksmbdtools.c
@@ -255,6 +255,30 @@ void terminate_ksmbd_daemon(void)
 	send_signal_to_ksmbd_daemon(SIGTERM);
 }
 
+int get_running_pid(void)
+{
+	char daemon_pid[10] = { 0 };
+	pid_t pid = 0;
+	int fd;
+
+	fd = open(KSMBD_LOCK_FILE, O_RDONLY);
+	if (fd < 0) {
+		pr_info("Can't open lock file %s: %s\n", KSMBD_LOCK_FILE, strerr(errno));
+		return -ENOENT;
+	}
+
+	if (read(fd, &daemon_pid, sizeof(daemon_pid)) == -1) {
+		pr_err("Unable to read lock file: %s\n", strerr(errno));
+		close(fd);
+		return -EINVAL;
+	}
+
+	close(fd);
+	pid = strtol(daemon_pid, NULL, 10);
+
+	return pid;
+}
+
 int test_file_access(char *conf)
 {
 	int fd = open(conf, O_RDWR | O_CREAT, S_IRWXU | S_IRGRP);
-- 
2.34.1


  parent reply	other threads:[~2022-03-07  1:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-07  1:33 [PATCH 0/9] Unify all programs into a single binary "ksmbdctl" Enzo Matsumiya
2022-03-07  1:33 ` [PATCH 1/9] ksmbd-tools: rename dirs to reflect new commands Enzo Matsumiya
2022-03-07  1:33 ` Enzo Matsumiya [this message]
2022-03-07  1:33 ` [PATCH 3/9] ksmbd-tools: use quotes for local includes Enzo Matsumiya
2022-03-07  1:33 ` [PATCH 4/9] share: introduce share_cmd Enzo Matsumiya
2022-03-10  2:19   ` Namjae Jeon
2022-03-07  1:33 ` [PATCH 5/9] user: introduce user_cmd Enzo Matsumiya
2022-03-10  2:17   ` Namjae Jeon
2022-03-07  1:33 ` [PATCH 6/9] daemon: introduce daemon_cmd Enzo Matsumiya
2022-03-07  1:33 ` [PATCH 7/9] daemon/rpc_samr: drop unused function rpc_samr_remove_domain_entry() Enzo Matsumiya
2022-03-07  1:33 ` [PATCH 8/9] Unify all programs into a single binary "ksmbdctl" Enzo Matsumiya
2022-03-07  1:33 ` [PATCH 9/9] README: change to markdown, updates for ksmbdctl Enzo Matsumiya
2022-03-10  2:11 ` [PATCH 0/9] Unify all programs into a single binary "ksmbdctl" Namjae Jeon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220307013344.29064-3-ematsumiya@suse.de \
    --to=ematsumiya@suse.de \
    --cc=hyc.lee@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=senozhatsky@chromium.org \
    --cc=sergey.senozhatsky@gmail.com \
    --cc=smfrench@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox