Browse Source

Add klish binary template

Serj Kalichev 7 months ago
parent
commit
cedb966742
10 changed files with 195 additions and 11 deletions
  1. 0 7
      .gitignore
  2. 3 1
      bin/Makefile.am
  3. 1 0
      bin/klish/.gitignore
  4. 10 0
      bin/klish/Makefile.am
  5. 40 0
      bin/klish/klish.c
  6. 118 0
      bin/klish/opts.c
  7. 18 0
      bin/klish/private.h
  8. 1 0
      bin/klishd/.gitignore
  9. 3 0
      bin/klishd/klishd.c
  10. 1 3
      bin/klishd/opts.c

+ 0 - 7
.gitignore

@@ -35,13 +35,6 @@
 /debian/klish-dev
 /debian/files
 
-# Binaries
-/bin/clish
-/bin/konf
-/bin/konfd
-/bin/sigexec
-/testc/testc
-
 # Docs
 /doc/*.html
 /doc/*.pdf

+ 3 - 1
bin/Makefile.am

@@ -1,4 +1,6 @@
 EXTRA_DIST += \
-	bin/klishd/Makefile.am
+	bin/klishd/Makefile.am \
+	bin/klish/Makefile.am
 
 include $(top_srcdir)/bin/klishd/Makefile.am
+include $(top_srcdir)/bin/klish/Makefile.am

+ 1 - 0
bin/klish/.gitignore

@@ -0,0 +1 @@
+/klish

+ 10 - 0
bin/klish/Makefile.am

@@ -0,0 +1,10 @@
+bin_PROGRAMS += \
+	bin/klish/klish
+
+bin_klish_klish_SOURCES = \
+	bin/klish/private.h \
+	bin/klish/opts.c \
+	bin/klish/klish.c
+
+bin_klish_klish_LDADD = \
+	libklish.la

+ 40 - 0
bin/klish/klish.c

@@ -0,0 +1,40 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <faux/faux.h>
+#include <faux/str.h>
+
+#include <klish/ktp.h>
+#include <klish/ktp_session.h>
+
+#include "private.h"
+
+
+int main(int argc, char **argv)
+{
+	int retval = -1;
+	struct options *opts = NULL;
+
+	// Parse command line options
+	opts = opts_init();
+	if (opts_parse(argc, argv, opts))
+		goto err;
+
+	retval = 0;
+
+err:
+	opts_free(opts);
+
+	return retval;
+}

+ 118 - 0
bin/klish/opts.c

@@ -0,0 +1,118 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <getopt.h>
+
+#include <faux/faux.h>
+#include <faux/str.h>
+
+#include <klish/ktp_session.h>
+
+#include "private.h"
+
+
+/** @brief Initialize option structure by defaults
+ */
+struct options *opts_init(void)
+{
+	struct options *opts = NULL;
+
+	opts = faux_zmalloc(sizeof(*opts));
+	assert(opts);
+
+	// Initialize
+	opts->verbose = BOOL_FALSE;
+	opts->socket = faux_str_dup(KLISH_DEFAULT_UNIX_SOCKET_PATH);
+
+	return opts;
+}
+
+
+/** @brief Free options structure
+ */
+void opts_free(struct options *opts)
+{
+	if (!opts)
+		return;
+	faux_str_free(opts->socket);
+	faux_free(opts);
+}
+
+
+/** @brief Parse command line options
+ */
+int opts_parse(int argc, char *argv[], struct options *opts)
+{
+	static const char *shortopts = "hvS:";
+	static const struct option longopts[] = {
+		{"socket",		1, NULL, 'S'},
+		{"help",		0, NULL, 'h'},
+		{"verbose",		0, NULL, 'v'},
+		{NULL,			0, NULL, 0}
+	};
+
+	optind = 1;
+	while(1) {
+		int opt = 0;
+
+		opt = getopt_long(argc, argv, shortopts, longopts, NULL);
+		if (-1 == opt)
+			break;
+		switch (opt) {
+		case 'S':
+			faux_str_free(opts->socket);
+			opts->socket = faux_str_dup(optarg);
+			break;
+		case 'v':
+			opts->verbose = BOOL_TRUE;
+			break;
+		case 'h':
+			help(0, argv[0]);
+			_exit(0);
+			break;
+		default:
+			help(-1, argv[0]);
+			_exit(-1);
+			break;
+		}
+	}
+
+	return 0;
+}
+
+/** @brief Print help message
+ */
+void help(int status, const char *argv0)
+{
+	const char *name = NULL;
+
+	if (!argv0)
+		return;
+
+	// Find the basename
+	name = strrchr(argv0, '/');
+	if (name)
+		name++;
+	else
+		name = argv0;
+
+	if (status != 0) {
+		fprintf(stderr, "Try `%s -h' for more information.\n",
+			name);
+	} else {
+		printf("Version : %s\n", VERSION);
+		printf("Usage   : %s [options]\n", name);
+		printf("Klish client\n");
+		printf("Options :\n");
+		printf("\t-S, --socket UNIX socket path.\n");
+		printf("\t-h, --help Print this help.\n");
+		printf("\t-v, --verbose Be verbose.\n");
+	}
+}

+ 18 - 0
bin/klish/private.h

@@ -0,0 +1,18 @@
+
+#ifndef VERSION
+#define VERSION "1.0.0"
+#endif
+
+
+/** @brief Command line and config file options
+ */
+struct options {
+	bool_t verbose;
+	char *socket;
+};
+
+// Options
+void help(int status, const char *argv0);
+struct options *opts_init(void);
+void opts_free(struct options *opts);
+int opts_parse(int argc, char *argv[], struct options *opts);

+ 1 - 0
bin/klishd/.gitignore

@@ -0,0 +1 @@
+/klishd

+ 3 - 0
bin/klishd/klishd.c

@@ -237,6 +237,7 @@ int main(int argc, char **argv)
 			int id = 0; // Event idenftifier
 			void *data = NULL; // Event data
 
+syslog(LOG_DEBUG, "Timeout\n");
 			// Some scheduled events
 			while(faux_sched_pop(sched, &id, &data) == 0) {
 				syslog(LOG_DEBUG, "sched: Update event\n");
@@ -249,6 +250,7 @@ int main(int argc, char **argv)
 		while ((pollfd = faux_pollfd_each_active(fds, &pollfd_iter))) {
 			int fd = pollfd->fd;
 
+syslog(LOG_DEBUG, "Listen\n");
 			// Listen socket
 			if (fd == listen_unix_sock) {
 				int new_conn = -1;
@@ -262,6 +264,7 @@ int main(int argc, char **argv)
 
 			// If it's not a listen socket then we have received
 			// a message from client.
+syslog(LOG_DEBUG, "Client\n");
 		}
 
 	} // Main loop end

+ 1 - 3
bin/klishd/opts.c

@@ -137,13 +137,11 @@ void help(int status, const char *argv0)
 	} else {
 		printf("Version : %s\n", VERSION);
 		printf("Usage   : %s [options]\n", name);
-		printf("Certificate Revocation Service Daemon\n");
+		printf("Klish daemon\n");
 		printf("Options :\n");
 		printf("\t-h, --help Print this help.\n");
 		printf("\t-f, --foreground Don't daemonize.\n");
 		printf("\t-v, --verbose Be verbose.\n");
-		printf("\t-s, --silent Be silent.\n");
-		printf("\t-k, --check Check config only and exit.\n");
 		printf("\t-p <path>, --pid=<path> File to save daemon's PID to ("
 			DEFAULT_PIDFILE ").\n");
 		printf("\t-c <path>, --conf=<path> Config file ("