Skip to content
Snippets Groups Projects
main.c 2.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* SPDX-License-Identifier: GPL-2.0 */
    /*
     * main.c - qosmngr's boilerplate and entry point
     *
     * Copyright (C) 2019 iopsys Software Solutions AB. All rights reserved.
     *
     * Author: Oskar Viljasaar <oskar.viljasaar@iopsys.eu>
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     * This program is distributed in the hope that it will be useful, but
     * WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
     * 02110-1301 USA
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <libubus.h>
    
    #include "qosmngr.h"
    #include "version.h"
    
    const char *ubus_socket;
    
    struct ubus_context *ctx = NULL;
    
    /* install a signal handler */
    int set_sighandler(int sig, void (*handler)(int))
    {
    	struct sigaction sa;
    
    	memset(&sa, 0, sizeof(sa));
    	sa.sa_flags = 0;
    	sigemptyset(&sa.sa_mask);
    	sa.sa_handler = handler;
    	if (sigaction(sig, &sa, NULL) < 0) {
    		fprintf(stderr, "Error sigaction %d\n", sig);
    		return -1;
    	}
    
    	return 0;
    }
    
    static int qosmngr_exit()
    {
    	ubus_free(ctx);
    	uloop_done();
    
    	exit(EXIT_SUCCESS);
    }
    
    static void qosmngr_sighandler(int sig)
    {
    	switch (sig) {
    	case SIGTERM:
    	case SIGINT:
    		qosmngr_exit();
    		break;
    	default:
    		break;
    	}
    }
    
    void qosmngr_version()
    {
    	fprintf(stderr, "version       : %s.%s\n", qosmngr_base_version, qosmngr_xtra_version);
    }
    
    int main(int argc, char **argv)
    {
    	int ret;
    	int ch;
    
    	while ((ch = getopt(argc, argv, "vs:e:")) != -1) {
    		switch (ch) {
    		case 'v':
    			qosmngr_version();
    			exit(0);
    		case 's':
    			ubus_socket = optarg;
    			break;
    		default:
    			break;
    		}
    	}
    
    	argc -= optind;
    	argv += optind;
    
    	uloop_init();
    	ctx = ubus_connect(ubus_socket);
    	if (!ctx) {
    		fprintf(stderr, "Failed to connect to ubus\n");
    		return -1;
    	}
    
    	ubus_add_uloop(ctx);
    
    	ret = qosmngr_publish_object(ctx);
    	if (ret)
    		goto out;
    
    	set_sighandler(SIGPIPE, SIG_DFL);
    	set_sighandler(SIGINT, qosmngr_sighandler);
    	set_sighandler(SIGTERM, qosmngr_sighandler);
    
    	/* Main loop of qosmngr */
    	uloop_run();
    
     out:
    	ubus_free(ctx);
    	uloop_done();
    
    	return 0;
    }