Browse Source

ismounted + showbits

David Marec 8 months ago
parent
commit
8068bc0034
5 changed files with 225 additions and 0 deletions
  1. 22 0
      ismounted/Makefile
  2. 142 0
      ismounted/ismounted.c
  3. 3 0
      kqueue/.gitignore
  4. 38 0
      showbits/showbits.c
  5. 20 0
      vrac/SixM.sh

+ 22 - 0
ismounted/Makefile

@@ -0,0 +1,22 @@
+CFLAGS+=-O3 -Wall -fPIE -fno-strict-aliasing
+CFLAGS+=-Wformat -Wformat=2 -Wconversion -Wimplicit-fallthrough
+CFLAGS+=-Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3
+CFLAGS+=-fstack-clash-protection -fstack-protector-strong
+CFLAGS+=-fstrict-flex-arrays=3
+
+LDFLAGS+=-Wl,-O3,--sort-common,--as-needed,-z,relro,-z,now,--strip-all -pie
+LDFLAGS+=-Wl,-z,nodlopen -Wl,-z,noexecstack,--no-copy-dt-needed-entries -lrt
+
+PRJ=ismounted
+PRJ_OBJS=ismounted.o
+
+.PHONY: clean
+.MAIN: $(PRJ)
+
+$(PRJ): $(PRJ_OBJS)
+	$(CC) -o ${.TARGET} ${.ALLSRC} $(LDFLAGS)
+%o:
+	$(CC) -o ${.TARGET} -c ${.IMPSRC}
+clean:
+	rm -f *.o $(PRJ)
+

+ 142 - 0
ismounted/ismounted.c

@@ -0,0 +1,142 @@
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <limits.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <locale.h>
+#include <err.h>
+#include <string.h>
+#include <errno.h>
+#include <getopt.h>
+#include <signal.h>
+
+static struct stat parent, current;
+
+static int period;
+const char *path;
+
+static void __attribute__((__noreturn__)) usage(void)
+{
+	fputs("\nUsage:\n", stderr);
+	fputs("ismounted [-w timeout] folder\n", stderr);
+	fputs("   -w: 1s periodic check until the process ran out of time.\n", stderr);
+
+	exit(EXIT_SUCCESS);
+}
+
+static void sigHandler(int signo,
+		siginfo_t *si __attribute__((unused)), void /* ucontext_t */ *uap __attribute__((unused)))
+{
+	switch (signo) {
+		case SIGINFO:
+			printf("Waiting for %s mount. %d seconds remaining.\n", path, period);
+			break;
+		case SIGALRM:
+			/* Timer */
+			break;
+		default:
+			break;
+	}
+}
+
+int main(int argc, char *argv[])
+{
+	int r;
+	char *p_folder = NULL,  *a_folder = NULL;
+	/* timer: optional */
+	struct itimerspec its;
+	timer_t timerid;
+	int ismounted = 0;
+
+	/* handle options */
+	int ch;
+	struct option longopts[] = {
+		{ "wait", required_argument, &period, 'w' },
+		{ NULL,	0, NULL, 0 }
+	};
+
+	opterr = 0;
+	while ((ch = getopt_long(argc, argv, "w:", longopts, NULL)) != -1) {
+		switch (ch) {
+			case 'w':
+				period = atoi(optarg);
+				break;
+			case 0:
+				/* long option */
+				break;
+		}
+	}
+
+	if (optind == (argc - 1))
+		path = argv[optind];
+	else
+		usage();
+
+	/* get real path name */
+	a_folder = realpath(path, NULL);
+	if (a_folder == NULL) 
+		err(EXIT_FAILURE, "Path <%s> unreachable.", path);
+
+	if (!strcmp("/", a_folder)) {
+		printf("That's the root filesystem dude.");
+		return EXIT_SUCCESS;
+	}
+
+	/* move to parent */
+	strncat(a_folder, "/../", PATH_MAX - 1);
+	p_folder = realpath(a_folder, NULL);
+
+	r = stat(p_folder, &parent);
+	if (r)
+		err(EXIT_FAILURE, "Parent folder of <%s> is unreachable.", path);
+
+	/* signal */
+	struct sigaction sa;
+	sa.sa_handler = NULL;
+	sa.sa_sigaction = &sigHandler;
+	sa.sa_flags = SA_SIGINFO;
+	sigemptyset(&sa.sa_mask);
+	sigfillset(&sa.sa_mask);
+	if (sigaction(SIGINFO, &sa, NULL)) {
+		warn("SIGNFO not caught.");
+	}
+	if (sigaction(SIGALRM, &sa, NULL)) {
+		err(EXIT_FAILURE, "sigaction:ALARM");
+	}
+
+	/* timer, if set */
+	if ((period > 0) ) {
+		its.it_interval.tv_sec = 1;
+		its.it_interval.tv_nsec = 0;
+		its.it_value.tv_sec = its.it_interval.tv_sec;
+		its.it_value.tv_nsec = 0;
+		if (timer_create(CLOCK_REALTIME, NULL, &timerid)) {
+			err(EXIT_FAILURE,"timer_create failed.");
+		}
+		if (timer_settime(timerid, 0, &its, NULL)) {
+			err(EXIT_FAILURE, "timer_settime failed.");
+		}
+	}
+
+	/* proceed */
+	printf("Waiting for folder %s to be mounted ...\n", path);
+	while (1) {
+		r = stat(path, &current);
+		if (r)
+			err(EXIT_FAILURE, "Current folder <%s> is unreachable.", path);
+
+		ismounted = (current.st_dev != parent.st_dev);
+		if (ismounted || (period == 0))
+			break;
+
+		/* timer */
+		pause();
+		--period;
+	}
+
+	printf("Folder path %s is %sa mount point.\n", path, (ismounted) ? "" : "*not* " );
+
+	return EXIT_SUCCESS;
+}
+

+ 3 - 0
kqueue/.gitignore

@@ -0,0 +1,3 @@
+# Ignore everything in this directory
+obj/*
+

+ 38 - 0
showbits/showbits.c

@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+/*
+ * cc showbits.c -O3 -o sb
+ */
+
+static void showbits(const uint32_t data)
+{
+	uint32_t mask = 1<<31;
+	size_t i = 31;
+
+	for(; i;)
+		printf("%02zu\u00B7", i--);
+
+	puts("00");
+
+	for(; mask; mask >>= 1 ) {
+		printf("%s", (uint32_t)(data & mask) ? "\u2731\u2731" : "__");
+		if (mask != 1)
+			printf("\u205E");
+	}
+
+	puts("");
+}
+
+int main(int argc, char *argv[])
+{
+	if (argc < 2)
+		exit(EXIT_FAILURE);
+
+	showbits(strtol(argv[1], NULL, 16));
+	puts("");
+
+	return EXIT_SUCCESS;
+}
+

+ 20 - 0
vrac/SixM.sh

@@ -0,0 +1,20 @@
+#!/bin/sh
+
+# Six month ago
+# -------------
+
+SIXM=`date -j -v"-6m" +"%F %T"`
+
+## Epoc 
+SIXM_EPOC=`date -j -f "%F %T" "$SIXM" "+%s"`
+
+## express SQL date into epoc time
+## 
+
+SQL_DATE_EPOC=`date -j -f "%F %T" "${1}" "+%s"`
+
+##  test sql_date < sixm
+
+[ "${SQL_DATE_EPOC}" -gt "$SIXM_EPOC" ] && echo "match"
+
+