about summary refs log tree commit diff
path: root/src/core/ppp/nm-ppp-manager-call.c
blob: 8c788fadb323d1834b0792a0b131ebc8dd5e74c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2016 Red Hat, Inc.
 */

#include "src/core/nm-default-daemon.h"

#include "nm-ppp-manager-call.h"

#include <sys/types.h>
#include <sys/stat.h>

#include "nm-manager.h"
#include "nm-core-utils.h"
#include "nm-ppp-plugin-api.h"

#define PPP_PLUGIN_PATH NMPLUGINDIR "/libnm-ppp-plugin.so"

/*****************************************************************************/

static const NMPPPOps *_ppp_ops = NULL;

#define ppp_ops_get() ((const NMPPPOps *) g_atomic_pointer_get(&_ppp_ops))

NMPPPManager *
nm_ppp_manager_create(const char *iface, GError **error)
{
    NMPPPManager   *ret;
    GModule        *plugin;
    GError         *error_local = NULL;
    struct stat     st;
    const NMPPPOps *ppp_ops;

again:
    ppp_ops = ppp_ops_get();
    if (G_UNLIKELY(!ppp_ops)) {
        if (stat(PPP_PLUGIN_PATH, &st) != 0) {
            g_set_error_literal(error,
                                NM_MANAGER_ERROR,
                                NM_MANAGER_ERROR_MISSING_PLUGIN,
                                "the PPP plugin " PPP_PLUGIN_PATH " is not installed");
            return NULL;
        }

        if (!nm_utils_validate_plugin(PPP_PLUGIN_PATH, &st, &error_local)) {
            g_set_error(error,
                        NM_MANAGER_ERROR,
                        NM_MANAGER_ERROR_MISSING_PLUGIN,
                        "could not load the PPP plugin " PPP_PLUGIN_PATH ": %s",
                        error_local->message);
            g_clear_error(&error_local);
            return NULL;
        }

        plugin = g_module_open(PPP_PLUGIN_PATH, G_MODULE_BIND_LOCAL);
        if (!plugin) {
            g_set_error(error,
                        NM_MANAGER_ERROR,
                        NM_MANAGER_ERROR_MISSING_PLUGIN,
                        "could not load the PPP plugin " PPP_PLUGIN_PATH ": %s",
                        g_module_error());
            return NULL;
        }

        if (!g_module_symbol(plugin, "ppp_ops", (gpointer *) &ppp_ops)) {
            g_set_error(error,
                        NM_MANAGER_ERROR,
                        NM_MANAGER_ERROR_MISSING_PLUGIN,
                        "error loading the PPP plugin: %s",
                        g_module_error());
            return NULL;
        }

        nm_assert(ppp_ops);
        nm_assert(ppp_ops->create);
        nm_assert(ppp_ops->start);
        nm_assert(ppp_ops->stop);
        nm_assert(ppp_ops->stop_cancel);

        if (!g_atomic_pointer_compare_and_exchange(&_ppp_ops, NULL, ppp_ops)) {
            g_module_close(plugin);
            goto again;
        }

        /* after loading glib types from the plugin, we cannot unload the library anymore.
         * Make it resident. */
        g_module_make_resident(plugin);

        nm_log_info(LOGD_CORE | LOGD_PPP, "loaded PPP plugin " PPP_PLUGIN_PATH);
    }

    ret = ppp_ops->create(iface);
    g_return_val_if_fail(ret, NULL);
    return ret;
}

gboolean
nm_ppp_manager_start(NMPPPManager *self,
                     NMActRequest *req,
                     const char   *ppp_name,
                     guint32       timeout_secs,
                     guint         baud_override,
                     GError      **err)
{
    const NMPPPOps *ppp_ops = ppp_ops_get();

    g_return_val_if_fail(ppp_ops, FALSE);

    return ppp_ops->start(self, req, ppp_name, timeout_secs, baud_override, err);
}

NMPPPManagerStopHandle *
nm_ppp_manager_stop(NMPPPManager            *self,
                    GCancellable            *cancellable,
                    NMPPPManagerStopCallback callback,
                    gpointer                 user_data)
{
    const NMPPPOps *ppp_ops = ppp_ops_get();

    g_return_val_if_fail(ppp_ops, NULL);

    return ppp_ops->stop(self, cancellable, callback, user_data);
}

void
nm_ppp_manager_stop_cancel(NMPPPManagerStopHandle *handle)
{
    const NMPPPOps *ppp_ops = ppp_ops_get();

    g_return_if_fail(ppp_ops);
    g_return_if_fail(handle);

    ppp_ops->stop_cancel(handle);
}