Blame clients/nm-online.c

Packit Service 87a54e
/* SPDX-License-Identifier: GPL-2.0-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2006 - 2008 Novell, Inc.
Packit 5756e2
 * Copyright (C) 2008 - 2014 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
/*
Packit 5756e2
 * nm-online.c - Are we online?
Packit 5756e2
 *
Packit 5756e2
 * Return values:
Packit 5756e2
 *
Packit 5756e2
 * 0: already online or connection established within given timeout
Packit 5756e2
 * 1: offline or not online within given timeout
Packit 5756e2
 * 2: unspecified error
Packit 5756e2
 *
Packit 5756e2
 * Robert Love <rml@novell.com>
Packit 5756e2
 */
Packit 5756e2
Packit Service 2bceb2
#include "libnm/nm-default-client.h"
Packit 5756e2
Packit 5756e2
#include <stdio.h>
Packit 5756e2
#include <stdlib.h>
Packit 5756e2
#include <getopt.h>
Packit 5756e2
#include <locale.h>
Packit 5756e2
Packit 5756e2
#include "nm-libnm-aux/nm-libnm-aux.h"
Packit 5756e2
Packit 5756e2
#define PROGRESS_STEPS 15
Packit 5756e2
Packit Service a1bd4f
#define EXIT_NONE                -1
Packit 5756e2
#define EXIT_FAILURE_OFFLINE     1
Packit 5756e2
#define EXIT_FAILURE_ERROR       2
Packit 5756e2
#define EXIT_FAILURE_LIBNM_BUG   42
Packit 5756e2
#define EXIT_FAILURE_UNSPECIFIED 43
Packit 5756e2
Packit Service a1bd4f
typedef struct {
Packit Service a1bd4f
    GMainLoop *   loop;
Packit Service a1bd4f
    NMClient *    client;
Packit Service a1bd4f
    GCancellable *client_new_cancellable;
Packit Service a1bd4f
    guint         client_new_timeout_id;
Packit Service a1bd4f
    guint         handle_timeout_id;
Packit Service a1bd4f
    gulong        client_notify_id;
Packit Service a1bd4f
    gboolean      exit_no_nm;
Packit Service a1bd4f
    gboolean      wait_startup;
Packit Service a1bd4f
    gboolean      quiet;
Packit Service a1bd4f
    gint64        start_timestamp_ms;
Packit Service a1bd4f
    gint64        end_timestamp_ms;
Packit Service a1bd4f
    gint64        progress_step_duration;
Packit Service a1bd4f
    int           retval;
Packit 5756e2
} OnlineData;
Packit 5756e2
Packit 5756e2
static gint64
Packit Service a1bd4f
_now_ms(void)
Packit 5756e2
{
Packit Service a1bd4f
    return g_get_monotonic_time() / (G_USEC_PER_SEC / 1000);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
_return(OnlineData *data, int retval)
Packit 5756e2
{
Packit Service a1bd4f
    nm_assert(data);
Packit Service a1bd4f
    nm_assert(data->retval == EXIT_FAILURE_UNSPECIFIED);
Packit 5756e2
Packit Service a1bd4f
    data->retval = retval;
Packit Service a1bd4f
    nm_clear_g_signal_handler(data->client, &data->client_notify_id);
Packit Service a1bd4f
    g_main_loop_quit(data->loop);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
_print_progress(gboolean wait_startup, int progress_next_step_i, gint64 remaining_ms, int retval)
Packit 5756e2
{
Packit Service a1bd4f
    int i, j;
Packit Service a1bd4f
Packit Service a1bd4f
    j = progress_next_step_i < 0 ? PROGRESS_STEPS : progress_next_step_i;
Packit Service a1bd4f
Packit Service a1bd4f
    g_print("\r%s", _("Connecting"));
Packit Service a1bd4f
    for (i = 0; i < PROGRESS_STEPS; i++)
Packit Service a1bd4f
        putchar(i < j ? '.' : ' ');
Packit Service a1bd4f
    g_print(" %4lds", (long) (MAX(0, remaining_ms + 999) / 1000));
Packit Service a1bd4f
    if (retval != EXIT_NONE) {
Packit Service a1bd4f
        const char *result;
Packit Service a1bd4f
Packit Service a1bd4f
        if (wait_startup) {
Packit Service a1bd4f
            if (retval == EXIT_SUCCESS)
Packit Service a1bd4f
                result = "started";
Packit Service a1bd4f
            else if (retval == EXIT_FAILURE_OFFLINE)
Packit Service a1bd4f
                result = "startup-pending";
Packit Service a1bd4f
            else
Packit Service a1bd4f
                result = "failure";
Packit Service a1bd4f
        } else {
Packit Service a1bd4f
            if (retval == EXIT_SUCCESS)
Packit Service a1bd4f
                result = "online";
Packit Service a1bd4f
            else
Packit Service a1bd4f
                result = "offline";
Packit Service a1bd4f
        }
Packit Service a1bd4f
Packit Service a1bd4f
        g_print(" [%s]\n", result);
Packit Service a1bd4f
    }
Packit Service a1bd4f
    fflush(stdout);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
quit_if_connected(OnlineData *data)
Packit 5756e2
{
Packit Service a1bd4f
    NMState state;
Packit Service a1bd4f
Packit Service a1bd4f
    state = nm_client_get_state(data->client);
Packit Service a1bd4f
    if (!nm_client_get_nm_running(data->client)) {
Packit Service a1bd4f
        if (data->exit_no_nm) {
Packit Service a1bd4f
            _return(data, EXIT_FAILURE_OFFLINE);
Packit Service a1bd4f
            return TRUE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    } else if (data->wait_startup) {
Packit Service a1bd4f
        if (!nm_client_get_startup(data->client)) {
Packit Service a1bd4f
            _return(data, EXIT_SUCCESS);
Packit Service a1bd4f
            return TRUE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    } else {
Packit Service a1bd4f
        if (state == NM_STATE_CONNECTED_LOCAL || state == NM_STATE_CONNECTED_SITE
Packit Service a1bd4f
            || state == NM_STATE_CONNECTED_GLOBAL) {
Packit Service a1bd4f
            _return(data, EXIT_SUCCESS);
Packit Service a1bd4f
            return TRUE;
Packit Service a1bd4f
        }
Packit Service a1bd4f
    }
Packit Service a1bd4f
    if (data->exit_no_nm && (state != NM_STATE_CONNECTING)) {
Packit Service a1bd4f
        _return(data, EXIT_FAILURE_OFFLINE);
Packit Service a1bd4f
        return TRUE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    return FALSE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
client_properties_changed(GObject *object, GParamSpec *pspec, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    quit_if_connected(user_data);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
handle_timeout(gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    OnlineData * data                 = user_data;
Packit Service a1bd4f
    const gint64 now                  = _now_ms();
Packit Service a1bd4f
    gint64       remaining_ms         = data->end_timestamp_ms - now;
Packit Service a1bd4f
    const gint64 elapsed_ms           = now - data->start_timestamp_ms;
Packit Service a1bd4f
    int          progress_next_step_i = 0;
Packit Service a1bd4f
Packit Service a1bd4f
    data->handle_timeout_id = 0;
Packit Service a1bd4f
Packit Service a1bd4f
    if (remaining_ms <= 3) {
Packit Service a1bd4f
        _return(data, EXIT_FAILURE_OFFLINE);
Packit Service a1bd4f
        return G_SOURCE_REMOVE;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (!data->quiet) {
Packit Service a1bd4f
        gint64 rem;
Packit Service a1bd4f
Packit Service a1bd4f
        /* calculate the next step (not the current): floor()+1 */
Packit Service a1bd4f
        progress_next_step_i =
Packit Service a1bd4f
            NM_MIN((elapsed_ms / data->progress_step_duration) + 1, PROGRESS_STEPS);
Packit Service a1bd4f
        _print_progress(data->wait_startup, progress_next_step_i, remaining_ms, EXIT_NONE);
Packit Service a1bd4f
Packit Service a1bd4f
        /* synchronize the timeout with the ticking of the seconds. */
Packit Service a1bd4f
        rem = remaining_ms % 1000;
Packit Service a1bd4f
        if (rem <= 3)
Packit Service a1bd4f
            rem = rem + G_USEC_PER_SEC;
Packit Service a1bd4f
        /* add small offset to awake a bit after the second ticks */
Packit Service a1bd4f
        remaining_ms = NM_MIN(remaining_ms, rem + 10);
Packit Service a1bd4f
Packit Service a1bd4f
        /* synchronize the timeout with the steps of the progress bar. */
Packit Service a1bd4f
        rem = (progress_next_step_i * data->progress_step_duration) - elapsed_ms;
Packit Service a1bd4f
        if (rem <= 3)
Packit Service a1bd4f
            rem = rem + data->progress_step_duration;
Packit Service a1bd4f
        /* add small offset to awake a bit after the second ticks */
Packit Service a1bd4f
        remaining_ms = NM_MIN(remaining_ms, rem + 10);
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    data->handle_timeout_id = g_timeout_add(remaining_ms, handle_timeout, data);
Packit Service a1bd4f
    return G_SOURCE_REMOVE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static gboolean
Packit Service a1bd4f
got_client_timeout(gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    OnlineData *data = user_data;
Packit 5756e2
Packit Service a1bd4f
    data->client_new_timeout_id = 0;
Packit Service a1bd4f
    data->quiet                 = TRUE;
Packit Service a1bd4f
    g_printerr(_("Error: timeout creating NMClient object\n"));
Packit Service a1bd4f
    _return(data, EXIT_FAILURE_LIBNM_BUG);
Packit Service a1bd4f
    return G_SOURCE_REMOVE;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static void
Packit Service a1bd4f
got_client(GObject *source_object, GAsyncResult *res, gpointer user_data)
Packit 5756e2
{
Packit Service a1bd4f
    OnlineData *  data          = user_data;
Packit Service a1bd4f
    gs_free_error GError *error = NULL;
Packit Service a1bd4f
Packit Service a1bd4f
    nm_assert(NM_IS_CLIENT(source_object));
Packit Service a1bd4f
    nm_assert(NM_CLIENT(source_object) == data->client);
Packit Service a1bd4f
Packit Service a1bd4f
    nm_clear_g_source(&data->client_new_timeout_id);
Packit Service a1bd4f
    g_clear_object(&data->client_new_cancellable);
Packit Service a1bd4f
Packit Service a1bd4f
    if (!g_async_initable_init_finish(G_ASYNC_INITABLE(source_object), res, &error)) {
Packit Service a1bd4f
        if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
Packit Service a1bd4f
            return;
Packit Service a1bd4f
        data->quiet = TRUE;
Packit Service a1bd4f
        g_printerr(_("Error: Could not create NMClient object: %s\n"), error->message);
Packit Service a1bd4f
        _return(data, EXIT_FAILURE_ERROR);
Packit Service a1bd4f
        return;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (quit_if_connected(data))
Packit Service a1bd4f
        return;
Packit Service a1bd4f
Packit Service a1bd4f
    data->client_notify_id =
Packit Service a1bd4f
        g_signal_connect(data->client, "notify", G_CALLBACK(client_properties_changed), data);
Packit Service a1bd4f
    data->handle_timeout_id =
Packit Service a1bd4f
        g_timeout_add(data->quiet ? NM_MAX(0, data->end_timestamp_ms - _now_ms()) : 0,
Packit Service a1bd4f
                      handle_timeout,
Packit Service a1bd4f
                      data);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int
Packit Service a1bd4f
main(int argc, char *argv[])
Packit 5756e2
{
Packit Service a1bd4f
    OnlineData data = {
Packit Service a1bd4f
        .retval = EXIT_FAILURE_UNSPECIFIED,
Packit Service a1bd4f
    };
Packit Service a1bd4f
    int             t_secs;
Packit Service a1bd4f
    GOptionContext *opt_ctx = NULL;
Packit Service a1bd4f
    gboolean        success;
Packit Service a1bd4f
    GOptionEntry    options[] = {
Packit Service a1bd4f
        {"quiet", 'q', 0, G_OPTION_ARG_NONE, &data.quiet, N_("Don't print anything"), NULL},
Packit Service a1bd4f
        {"wait-for-startup",
Packit Service a1bd4f
         's',
Packit Service a1bd4f
         0,
Packit Service a1bd4f
         G_OPTION_ARG_NONE,
Packit Service a1bd4f
         &data.wait_startup,
Packit Service a1bd4f
         N_("Wait for NetworkManager startup instead of a connection"),
Packit Service a1bd4f
         NULL},
Packit Service a1bd4f
        {"timeout",
Packit Service a1bd4f
         't',
Packit Service a1bd4f
         0,
Packit Service a1bd4f
         G_OPTION_ARG_INT,
Packit Service a1bd4f
         &t_secs,
Packit Service a1bd4f
         N_("Time to wait for a connection, in seconds (without the option, default value is 30)"),
Packit Service a1bd4f
         "<timeout>"},
Packit Service a1bd4f
        {"exit",
Packit Service a1bd4f
         'x',
Packit Service a1bd4f
         0,
Packit Service a1bd4f
         G_OPTION_ARG_NONE,
Packit Service a1bd4f
         &data.exit_no_nm,
Packit Service a1bd4f
         N_("Exit immediately if NetworkManager is not running or connecting"),
Packit Service a1bd4f
         NULL},
Packit Service a1bd4f
        {NULL},
Packit Service a1bd4f
    };
Packit Service a1bd4f
Packit Service a1bd4f
    /* Set locale to be able to use environment variables */
Packit Service a1bd4f
    setlocale(LC_ALL, "");
Packit Service a1bd4f
Packit Service a1bd4f
    bindtextdomain(GETTEXT_PACKAGE, NMLOCALEDIR);
Packit Service a1bd4f
    bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
Packit Service a1bd4f
    textdomain(GETTEXT_PACKAGE);
Packit Service a1bd4f
Packit Service a1bd4f
    t_secs = _nm_utils_ascii_str_to_int64(g_getenv("NM_ONLINE_TIMEOUT"), 10, 0, G_MAXINT, 30);
Packit Service a1bd4f
Packit Service a1bd4f
    data.start_timestamp_ms = _now_ms();
Packit Service a1bd4f
Packit Service a1bd4f
    opt_ctx = g_option_context_new(NULL);
Packit Service a1bd4f
    g_option_context_set_translation_domain(opt_ctx, GETTEXT_PACKAGE);
Packit Service a1bd4f
    g_option_context_set_ignore_unknown_options(opt_ctx, FALSE);
Packit Service a1bd4f
    g_option_context_set_help_enabled(opt_ctx, TRUE);
Packit Service a1bd4f
    g_option_context_add_main_entries(opt_ctx, options, NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    g_option_context_set_summary(
Packit Service a1bd4f
        opt_ctx,
Packit Service a1bd4f
        _("Waits for NetworkManager to finish activating startup network connections."));
Packit Service a1bd4f
Packit Service a1bd4f
    success = g_option_context_parse(opt_ctx, &argc, &argv, NULL);
Packit Service a1bd4f
    g_option_context_free(opt_ctx);
Packit Service a1bd4f
Packit Service a1bd4f
    if (!success) {
Packit Service a1bd4f
        g_printerr("%s: %s\n",
Packit Service a1bd4f
                   argv[0],
Packit Service a1bd4f
                   _("Invalid option.  Please use --help to see a list of valid options."));
Packit Service a1bd4f
        return EXIT_FAILURE_ERROR;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (t_secs < 0 || t_secs > 3600) {
Packit Service a1bd4f
        g_printerr("%s: %s\n",
Packit Service a1bd4f
                   argv[0],
Packit Service a1bd4f
                   _("Invalid option.  Please use --help to see a list of valid options."));
Packit Service a1bd4f
        return EXIT_FAILURE_ERROR;
Packit Service a1bd4f
    }
Packit Service a1bd4f
Packit Service a1bd4f
    if (t_secs == 0)
Packit Service a1bd4f
        data.quiet = TRUE;
Packit Service a1bd4f
Packit Service a1bd4f
    data.loop = g_main_loop_new(NULL, FALSE);
Packit Service a1bd4f
Packit Service a1bd4f
    data.end_timestamp_ms       = data.start_timestamp_ms + (t_secs * 1000);
Packit Service a1bd4f
    data.progress_step_duration = NM_MAX(
Packit Service a1bd4f
        1,
Packit Service a1bd4f
        (data.end_timestamp_ms - data.start_timestamp_ms + PROGRESS_STEPS / 2) / PROGRESS_STEPS);
Packit Service a1bd4f
Packit Service a1bd4f
    data.client_new_cancellable = g_cancellable_new();
Packit Service a1bd4f
Packit Service a1bd4f
    data.client_new_timeout_id = g_timeout_add_seconds(30, got_client_timeout, &data);
Packit Service a1bd4f
Packit Service a1bd4f
    data.client = nmc_client_new_async(data.client_new_cancellable,
Packit Service a1bd4f
                                       got_client,
Packit Service a1bd4f
                                       &data,
Packit Service a1bd4f
                                       NM_CLIENT_INSTANCE_FLAGS,
Packit Service a1bd4f
                                       (guint) NM_CLIENT_INSTANCE_FLAGS_NO_AUTO_FETCH_PERMISSIONS,
Packit Service a1bd4f
                                       NULL);
Packit Service a1bd4f
Packit Service a1bd4f
    g_main_loop_run(data.loop);
Packit Service a1bd4f
Packit Service a1bd4f
    nm_clear_g_cancellable(&data.client_new_cancellable);
Packit Service a1bd4f
    nm_clear_g_source(&data.client_new_timeout_id);
Packit Service a1bd4f
    nm_clear_g_source(&data.handle_timeout_id);
Packit Service a1bd4f
    nm_clear_g_signal_handler(data.client, &data.client_notify_id);
Packit Service a1bd4f
    g_clear_object(&data.client);
Packit Service a1bd4f
Packit Service a1bd4f
    nm_clear_pointer(&data.loop, g_main_loop_unref);
Packit Service a1bd4f
Packit Service a1bd4f
    if (!data.quiet)
Packit Service a1bd4f
        _print_progress(data.wait_startup,
Packit Service a1bd4f
                        -1,
Packit Service a1bd4f
                        NM_MAX(0, data.end_timestamp_ms - _now_ms()),
Packit Service a1bd4f
                        data.retval);
Packit Service a1bd4f
Packit Service a1bd4f
    return data.retval;
Packit 5756e2
}