Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config dir, log file and lock file command line options #70

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,10 +877,10 @@ void config_free()
};
}

void config_generate()
void config_generate(const char *config_dir)
{
struct dirent *ent;
DIR *dh = opendir(CONFIG_DIR);
DIR *dh = opendir(config_dir);

if (!dh) {
perror("opendir");
Expand All @@ -895,7 +895,7 @@ void config_generate()
continue;


sprintf(path, "%s/%s", CONFIG_DIR, ent->d_name);
sprintf(path, "%s/%s", config_dir, ent->d_name);

cfg = calloc(1, sizeof(struct keyboard_config));

Expand Down
119 changes: 71 additions & 48 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include <time.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include "keys.h"
#include "config.h"

Expand Down Expand Up @@ -1125,11 +1126,11 @@ static void cleanup()
udev_monitor_unref(udevmon);
}

static void lock()
static void lock(const char *lock_file)
{
int fd;

if ((fd = open(LOCK_FILE, O_CREAT | O_RDWR, 0600)) == -1) {
if ((fd = open(lock_file, O_CREAT | O_RDWR, 0600)) == -1) {
perror("flock open");
exit(1);
}
Expand All @@ -1150,12 +1151,12 @@ static void exit_signal_handler(int sig)
exit(0);
}

static void daemonize()
static void daemonize(const char *log_file)
{
int fd = open(LOG_FILE, O_APPEND | O_WRONLY);
int fd = open(log_file, O_APPEND | O_WRONLY);

info("Daemonizing.");
info("Log output will be stored in %s", LOG_FILE);
info("Log output will be stored in %s", log_file);

if (fork())
exit(0);
Expand All @@ -1178,57 +1179,79 @@ int main(int argc, char *argv[])
dbg("Debug mode enabled.");
dbg2("Verbose debugging enabled.");

if (argc > 1) {
if (!strcmp(argv[1], "-v")) {
fprintf(stderr, "keyd version: %s (%s)\n", VERSION,
GIT_COMMIT_HASH);
return 0;
} else if (!strcmp(argv[1], "-m")) {
return monitor_loop();
} else if (!strcmp(argv[1], "-l")) {
size_t i;

for (i = 0; i < KEY_MAX; i++)
if (keycode_table[i].name) {
const struct keycode_table_ent *ent
= &keycode_table[i];
printf("%s\n", ent->name);
if (ent->alt_name)
printf("%s\n",
ent->alt_name);
if (ent->shifted_name)
printf("%s\n",
ent->shifted_name);
}
return 0;
} else {
if (strcmp(argv[1], "-h")
&& strcmp(argv[1], "--help"))
char *config_dir = CONFIG_DIR;
char *log_file = LOG_FILE;
char *lock_file = LOCK_FILE;
{
static struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"config-dir", required_argument, NULL, 0},
{"log-file", required_argument, NULL, 1},
{"lock-file", required_argument, NULL, 2},
{NULL, 0, NULL, 0}
};
int longindex = 0;
int opt;
while ((opt = getopt_long(argc, argv, "vmlhd", longopts, &longindex)) != -1)
switch (opt) {
case 'v':
fprintf(stderr, "keyd version: %s (%s)\n", VERSION, GIT_COMMIT_HASH);
return 0;
case 'm':
return monitor_loop();
case 'l':
size_t i;

for (i = 0; i < KEY_MAX; i++)
if (keycode_table[i].name) {
const struct keycode_table_ent *ent = &keycode_table[i];
printf("%s\n", ent->name);
if (ent->alt_name)
printf("%s\n", ent->alt_name);
if (ent->shifted_name)
printf("%s\n", ent->shifted_name);
}
return 0;
case 'h':
fprintf(stderr,
"%s is not a valid option.\n",
argv[1]);

fprintf(stderr,
"Usage: %s [-m] [-l] [-d]\n\nOptions:\n"
"\t-m monitor mode\n" "\t-l list keys\n"
"\t-d fork and start as a daemon\n"
"\t-v print version\n"
"\t-h print this help message\n", argv[0]);

return 0;
}
"Usage: %s [-m] [-l] [-d]\n\nOptions:\n"
"\t-m monitor mode\n"
"\t-l list keys\n"
"\t-d fork and start as a daemon\n"
"\t--config-dir <CONFIG_DIR>\n"
"\t--log-file <LOG_FILE>\n"
"\t--lock-file <LOCK_FILE>\n"
"\t-v print version\n"
"\t-h print this help message\n",
argv[0]);
return 0;
case 'd':
daemonize(log_file);
break;
case 0:
config_dir = strdup(optarg);
fprintf(stderr, "Using config directory \"%s\"\n", config_dir);
break;
case 1:
log_file = strdup(optarg);
fprintf(stderr, "Using log file \"%s\"\n", log_file);
break;
case 2:
lock_file = strdup(optarg);
fprintf(stderr, "Using lock file \"%s\"\n", lock_file);
break;
case '?':
return 1;
}
}

lock();
lock(lock_file);

signal(SIGINT, exit_signal_handler);
signal(SIGTERM, exit_signal_handler);

if (argc > 1 && !strcmp(argv[1], "-d"))
daemonize();

info("Starting keyd v%s (%s).", VERSION, GIT_COMMIT_HASH);
config_generate();
config_generate(config_dir);
vkbd = create_virtual_keyboard();
vptr = create_virtual_pointer();

Expand Down