2017-08-23 20:57:44 +02:00
|
|
|
#include "../../core/communication.h"
|
|
|
|
#include "../../core/process.h"
|
|
|
|
#include "../../core/error.h"
|
2017-08-25 11:42:43 +02:00
|
|
|
#include "../lib/remoted.h"
|
2017-08-23 20:57:44 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-08-26 19:42:54 +02:00
|
|
|
#include "../../core/logger.h"
|
|
|
|
|
2017-08-23 20:57:44 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2017-08-28 00:03:35 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-08-23 20:57:44 +02:00
|
|
|
// to quit them properly if a signal occurs
|
|
|
|
struct service srv;
|
|
|
|
|
|
|
|
void handle_signal (int signalnumber)
|
|
|
|
{
|
|
|
|
// the application will shut down, and remove the service unix socket
|
|
|
|
if (srv_close (&srv) < 0) {
|
|
|
|
handle_error("srv_close < 0");
|
|
|
|
}
|
|
|
|
|
2017-08-26 19:42:54 +02:00
|
|
|
log_info ("remoted received a signal %d\n", signalnumber);
|
2017-08-23 20:57:44 +02:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2017-08-28 00:03:35 +02:00
|
|
|
void usage ()
|
|
|
|
{
|
|
|
|
fprintf (stderr, "remoted [-d <unix-socket-dir>] [-h]\n");
|
|
|
|
}
|
|
|
|
|
2017-08-26 19:42:54 +02:00
|
|
|
/* TODO: handle command line arguments */
|
2017-08-28 00:03:35 +02:00
|
|
|
|
|
|
|
// cmdline: remoted -d <unix-socket-dir>
|
2017-08-26 19:42:54 +02:00
|
|
|
void remoted_cmd_args (int argc, char **argv, char **env
|
|
|
|
, struct remoted_ctx *ctx)
|
|
|
|
{
|
|
|
|
(void) env;
|
|
|
|
(void) ctx;
|
2017-08-28 00:03:35 +02:00
|
|
|
|
|
|
|
int c;
|
|
|
|
while ( (c = getopt(argc, argv, "hd:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
ctx->unix_socket_dir = malloc (strlen (optarg) +1);
|
|
|
|
strncpy (ctx->unix_socket_dir, optarg, strlen (optarg));
|
|
|
|
log_debug ("remoted unix socket dir: %s", ctx->unix_socket_dir);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
|
|
|
usage ();
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
default:
|
|
|
|
log_debug ("remoted getopt returned character code 0%o ??\n", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc) {
|
|
|
|
log_debug ("remoted non-option ARGV-elements:");
|
|
|
|
while (optind < argc)
|
|
|
|
log_debug ("\t%s", argv[optind++]);
|
|
|
|
}
|
2017-08-26 19:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: handle authorizations */
|
|
|
|
int remoted_auth_conf (struct remoted_ctx *ctx)
|
2017-08-26 01:07:29 +02:00
|
|
|
{
|
2017-08-26 19:42:54 +02:00
|
|
|
(void) ctx;
|
|
|
|
return 0;
|
2017-08-26 01:07:29 +02:00
|
|
|
}
|
2017-08-23 20:57:44 +02:00
|
|
|
|
2017-08-28 00:03:35 +02:00
|
|
|
int main(int argc, char **argv, char **env)
|
2017-08-23 20:57:44 +02:00
|
|
|
{
|
2017-08-26 19:42:54 +02:00
|
|
|
struct remoted_ctx ctx;
|
|
|
|
memset (&ctx, 0, sizeof (struct remoted_ctx));
|
|
|
|
|
2017-08-23 20:57:44 +02:00
|
|
|
memset (&srv, 0, sizeof (struct service));
|
|
|
|
srv.index = 0;
|
|
|
|
srv.version = 0;
|
|
|
|
|
|
|
|
signal(SIGHUP, handle_signal);
|
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
signal(SIGQUIT, handle_signal);
|
|
|
|
|
2017-08-26 19:42:54 +02:00
|
|
|
remoted_cmd_args (argc, argv, env, &ctx);
|
|
|
|
|
|
|
|
log_info ("remoted started");
|
|
|
|
// load configuration
|
|
|
|
if (remoted_auth_conf (&ctx)) {
|
|
|
|
log_error ("remoted cannot load configuration");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log_info ("remoted configuration loaded");
|
2017-08-23 20:57:44 +02:00
|
|
|
|
|
|
|
if (srv_init (argc, argv, env, &srv, REMOTED_SERVICE_NAME) < 0) {
|
|
|
|
handle_error("srv_init < 0");
|
|
|
|
}
|
2017-08-26 19:42:54 +02:00
|
|
|
log_info ("remoted listening on %s", srv.spath);
|
2017-08-23 20:57:44 +02:00
|
|
|
|
2017-08-26 19:42:54 +02:00
|
|
|
// TODO: here comes pledge (openbsd)
|
2017-08-23 20:57:44 +02:00
|
|
|
|
|
|
|
// the service will loop until the end of time, a specific message, a signal
|
2017-08-28 00:03:35 +02:00
|
|
|
remoted_main_loop (&srv, &ctx);
|
2017-08-23 20:57:44 +02:00
|
|
|
|
2017-08-26 01:07:29 +02:00
|
|
|
// the application will shut down, and remove the service unix socket
|
2017-08-23 20:57:44 +02:00
|
|
|
if (srv_close (&srv) < 0) {
|
|
|
|
handle_error("srv_close < 0");
|
|
|
|
}
|
2017-08-26 19:42:54 +02:00
|
|
|
log_info ("remoted ended");
|
2017-08-23 20:57:44 +02:00
|
|
|
|
2017-08-28 00:03:35 +02:00
|
|
|
remoted_free_ctx (&ctx);
|
|
|
|
|
2017-08-23 20:57:44 +02:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|