David Marec hace 10 meses
padre
commit
5e7e840c78
Se han modificado 3 ficheros con 53 adiciones y 26 borrados
  1. 10 3
      kqueue/Makefile
  2. 38 23
      kqueue/kq_timer.c
  3. 5 0
      kqueue/obj/.gitignore

+ 10 - 3
kqueue/Makefile

@@ -1,10 +1,17 @@
-CFLAGS+=-O3 -Wall
-LDFLAGS+=-Wl,-O3,--sort-common,--as-needed,-z,relro,-z,now,--strip-all
+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
 
 KQT=kq_timer
 KQT_OBJS=kq_timer.o
 
-all: $(KQT)
+.PHONY: clean
+.MAIN: $(KQT)
 
 kq_timer: $(KQT_OBJS)
 	$(CC) -o ${.TARGET} ${.ALLSRC} $(LDFLAGS)

+ 38 - 23
kqueue/kq_timer.c

@@ -11,17 +11,35 @@
 #include <stdbool.h>
 
 static int verbose;
+const struct sched_param param = {.sched_priority = 15};
+static struct option longopts[] = {
+	{ "period",   required_argument,      NULL, 'p' },
+	{ "verbose",  no_argument      ,  &verbose,  1  },
+	{ NULL,	0, NULL, 0 }
+};
+
+void fd_close(int *fd) {
+	close(*fd);
+	*fd = -1;
+}
+#define _smartfd __attribute((cleanup(fd_close)))
+
+void file_close(FILE **fd) {
+	fclose(*fd);
+	*fd = NULL;
+}
+#define _smartfile __attribute((cleanup(file_close)))
 
-static int usage(void)
+[[noreturn]] static int usage(void)
 {
 	fputs("kq_timer [--verbose] [--period ms]\n", stderr);
 	exit (EXIT_SUCCESS);
 }
 
-static double diff_ms(const struct timespec *time1, const struct timespec *time0)
+__attribute__((const)) static double diff_ms(const struct timespec * restrict time1, const struct timespec * restrict time0)
 {
-	return (time1->tv_sec - time0->tv_sec) * 1000 
-		+ (time1->tv_nsec - time0->tv_nsec) / 1000000.0;
+	return (double)(time1->tv_sec - time0->tv_sec) * 1000.0
+		+ (double)(time1->tv_nsec - time0->tv_nsec) / 1000000.0;
 }
 
 static void sig_handler(int sig)
@@ -33,22 +51,17 @@ int main(int argc, char *argv[])
 {
 	struct kevent change; /* event we want to monitor */
 	struct kevent event;  /* event that was triggered */
-	int kq, nev; /* handlers */
+	_smartfd int kq;
+	int nev; /* handlers */
 	int ch; /* opt long */
 
 	int period = 1000; /* Default to 1 sec. */
 	size_t counter = 0;
 	size_t total = 0;
-	FILE *fd = NULL;
+	_smartfile FILE *fd = NULL;
 	char template[64];
 	int rc = EXIT_FAILURE;
 	struct	timespec t0 = { 0, 0};
-	const struct sched_param param = {.sched_priority = 49};
-	static struct option longopts[] = {
-		{ "period",   required_argument,      NULL, 'p' },
-		{ "verbose",  no_argument      ,  &verbose,  1  },
-		{ NULL,	0, NULL, 0 }
-	};
 
 	while ((ch = getopt_long(argc, argv, "hvp:", longopts, NULL)) != -1) {
 		switch (ch) {
@@ -83,19 +96,21 @@ int main(int argc, char *argv[])
 	strncpy(template, "/tmp/timer.XXXXXX", sizeof template);
 	if (mkstemp(template) == -1) {
 		warn("mkstemp() failure");
-		goto exit_kqueue;
+		return EXIT_FAILURE;
 	}
 
 	fd = fopen(template, "w");
 	if (fd == NULL) {
 		warn("fopen() failure");
-		goto exit_kqueue;
+		return EXIT_FAILURE;
 	}
 
 	printf("Log file: %s\n", template);
 
 	/* process priority */
-	sched_setscheduler(0, SCHED_FIFO, &param);
+	if (-1 == sched_setscheduler(0, SCHED_RR, &param)) {
+		warn("set scheduler() failure");
+	}
 
 	signal(SIGINT, sig_handler);
 
@@ -113,13 +128,15 @@ int main(int argc, char *argv[])
 			struct	timespec t1;
 			clock_gettime(CLOCK_MONOTONIC, &t1);
 
-			if (!(t0.tv_sec == 0 && t0.tv_nsec == 0))
+			if (!(t0.tv_sec == 0 && t0.tv_nsec == 0)) {
 				fprintf(fd, "%06.3f\n", diff_ms(&t1, &t0));
+				++total;
+			}
 
 			t0 = t1;
 
 			if (event.flags & EV_ERROR) {
-				fprintf(stderr, "EV_ERROR: %s\n", strerror(event.data));
+				fprintf(stderr, "EV_ERROR: %s\n", strerror((int)event.data));
 				rc = EXIT_FAILURE;
 				break;
 			}
@@ -133,17 +150,15 @@ int main(int argc, char *argv[])
 				}
 
 				counter = 0;
-				++total;
 			}
 		}
 	}
 
-	rc = 0;
+	rc = EXIT_SUCCESS;
 
-	fclose(fd);
 	fprintf(stdout, "\n%zu entries stored.\n", total);
-exit_kqueue:
-	close(kq);
-	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+	fprintf(stdout, "%zu cycles counted.\n", counter);
+
+	return rc;
 }
 

+ 5 - 0
kqueue/obj/.gitignore

@@ -0,0 +1,5 @@
+# No not push object files
+*
+# Except this file
+!.gitignore
+