Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • 0x4A6F-master
  • 0x4A6F-rpi4
  • autinerd/experimental-openwrt-24.10
  • experimental
  • feature/addMikrotikwAP
  • master
  • nrb/airmax-test
  • nrb/ar9344-reset-sequence
  • nrb/ex400-remove-wps
  • nrb/gluon-master-cpe510
  • nrb/test-radv-filter
  • nrbffs/fastd-remove-delay
  • nrbffs/netgear-ex6120
  • v2018.2.2-ffs
  • v2018.2.3-ffs
  • v2019.1-ffs
  • v2019.1.1-ffs
  • v2019.1.2-ffs
  • v2020.1-ffs
  • v2020.1.1-ffs
  • v2020.1.3-ffs
  • v2020.2-ffs
  • v2020.2.1-ffs
  • v2020.2.2-ffs
  • v2020.2.3-ffs
  • v2021.1-ffs
  • v2021.1.1-ffs
  • v2021.1.2-ffs
  • v2022.1.1-ffs
  • v2022.1.3-ffs
  • v2022.1.4-ffs
  • v2023.1-ffs
  • v2023.2-ffs
  • v2023.2.2-ffs
  • v2023.2.3-ffs
  • v2023.2.4-ffs
  • v2023.2.5-ffs
  • experimental-2022-09-24
  • experimental-2022-09-24-base
  • experimental-2023-03-11
  • experimental-2023-03-11-base
  • experimental-2023-03-12
  • experimental-2023-03-12-base
  • experimental-2023-03-16
  • experimental-2023-03-16-base
  • experimental-2023-03-20
  • experimental-2023-03-20-base
  • experimental-2023-03-23
  • experimental-2023-03-23-base
  • experimental-2023-03-25
  • experimental-2023-03-25-base
  • experimental-2023-03-26
  • experimental-2023-03-26-base
  • experimental-2023-03-30
  • experimental-2023-03-30-base
  • experimental-2023-03-31
  • experimental-2023-03-31-base
  • experimental-2023-04-01
  • experimental-2023-04-01-base
  • experimental-2023-04-08
  • experimental-2023-04-08-base
  • experimental-2023-04-10
  • experimental-2023-04-10-base
  • experimental-2023-04-13
  • experimental-2023-04-13-base
  • experimental-2023-04-15
  • experimental-2023-04-15-base
  • experimental-2023-04-16
  • experimental-2023-04-16-base
  • experimental-2023-04-18
  • experimental-2023-04-18-base
  • experimental-2023-04-20
  • experimental-2023-04-20-base
  • experimental-2023-04-26
  • experimental-2023-04-26-base
  • experimental-2023-04-28
  • experimental-2023-04-28-base
  • experimental-2023-04-30
  • experimental-2023-04-30-base
  • experimental-2023-05-02
  • experimental-2023-05-02-base
  • experimental-2023-05-03
  • experimental-2023-05-03-base
  • experimental-2023-05-12
  • experimental-2023-05-12-base
  • experimental-2023-05-21
  • experimental-2023-05-21-base
  • experimental-2023-05-25
  • experimental-2023-05-25-base
  • experimental-2023-07-02
  • experimental-2023-07-02-base
  • experimental-2023-07-04
  • experimental-2023-07-04-base
  • experimental-2023-07-12
  • experimental-2023-07-12-base
  • experimental-2023-07-16
  • experimental-2023-07-16-base
  • experimental-2023-08-04
  • experimental-2023-08-04-base
  • experimental-2023-08-10
  • experimental-2023-08-10-base
  • experimental-2023-09-08
  • experimental-2023-09-08-base
  • experimental-2023-09-09
  • experimental-2023-09-09-base
  • experimental-2023-09-10
  • experimental-2023-09-10-base
  • experimental-2023-09-11
  • experimental-2023-09-11-base
  • experimental-2023-09-12
  • experimental-2023-09-12-base
  • experimental-2023-09-13
  • experimental-2023-09-13-base
  • experimental-2023-09-15
  • experimental-2023-09-15-base
  • experimental-2023-09-16
  • experimental-2023-09-16-base
  • experimental-2023-09-18
  • experimental-2023-09-18-base
  • experimental-2023-09-20
  • experimental-2023-09-20-base
  • experimental-2023-09-27
  • experimental-2023-09-27-base
  • experimental-2023-09-28
  • experimental-2023-09-28-base
  • experimental-2023-09-29
  • experimental-2023-09-29-base
  • experimental-2023-10-02
  • experimental-2023-10-02-base
  • experimental-2023-10-13
  • experimental-2023-10-13-base
  • experimental-2023-10-14
  • experimental-2023-10-14-base
  • experimental-2023-10-16
  • experimental-2023-10-16-base
  • experimental-2023-10-23
  • experimental-2023-10-23-base
137 results

Target

Select target project
  • firmware/gluon
  • 0x4A6F/gluon
  • patrick/gluon
3 results
Select Git revision
  • 0x4A6F-master
  • 0x4A6F-rpi4
  • 2014.3.x
  • 2014.4.x
  • babel
  • hoodselector
  • master
  • radv-filterd
  • v2015.1.x
  • v2016.1.x
  • v2016.2.4-batmanbug
  • v2016.2.x
  • v2018.2.2-ffs
  • v2018.2.x
  • v2014.1
  • v2014.2
  • v2014.3
  • v2014.3.1
  • v2014.4
  • v2015.1
  • v2015.1.1
  • v2015.1.2
  • v2016.1
  • v2016.1.1
  • v2016.1.2
  • v2016.1.3
  • v2016.1.4
  • v2016.1.5
  • v2016.1.6
  • v2016.2
  • v2016.2.1
  • v2016.2.2
  • v2016.2.3
  • v2016.2.4
  • v2016.2.5
  • v2016.2.6
  • v2016.2.7
  • v2017.1
  • v2017.1.1
  • v2017.1.2
  • v2017.1.3
  • v2017.1.4
  • v2017.1.5
  • v2017.1.6
  • v2017.1.7
  • v2017.1.8
  • v2018.1
  • v2018.1.1
  • v2018.1.2
  • v2018.1.3
  • v2018.1.4
  • v2018.2
  • v2018.2-ffs0.1
  • v2018.2.1
  • v2018.2.1-ffs0.1
  • v2018.2.2-ffs0.1
56 results
Show changes
Showing
with 713 additions and 347 deletions
......@@ -2,5 +2,7 @@ all: respondd.so
CFLAGS += -Wall
respondd.so: respondd.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -lgluonutil -lplatforminfo -luci
SOURCES = respondd.c respondd-nodeinfo.c respondd-statistics.c respondd-neighbours.c
respondd.so: $(SOURCES) respondd-common.h
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -fvisibility=hidden -D_GNU_SOURCE -o $@ $(SOURCES) $(LDLIBS) -lgluonutil -lplatforminfo -luci -liwinfo
/* SPDX-FileCopyrightText: 2016-2019, Matthias Schiffer <mschiffer@universe-factory.net> */
/* SPDX-License-Identifier: BSD-2-Clause */
#pragma once
#define MAX_INACTIVITY 60000
struct json_object * respondd_provider_nodeinfo(void);
struct json_object * respondd_provider_statistics(void);
struct json_object * respondd_provider_neighbours(void);
/* SPDX-FileCopyrightText: 2016-2019, Matthias Schiffer <mschiffer@universe-factory.net> */
/* SPDX-License-Identifier: BSD-2-Clause */
#include "respondd-common.h"
#include <libgluonutil.h>
#include <iwinfo.h>
#include <json-c/json.h>
static struct json_object * get_wifi_neighbours(const char *ifname) {
const struct iwinfo_ops *iw = iwinfo_backend(ifname);
if (!iw)
return NULL;
int len;
char buf[IWINFO_BUFSIZE];
if (iw->assoclist(ifname, buf, &len) < 0)
return NULL;
struct json_object *neighbours = json_object_new_object();
struct iwinfo_assoclist_entry *entry;
for (entry = (struct iwinfo_assoclist_entry *)buf; (char*)(entry+1) <= buf + len; entry++) {
if (entry->inactive > MAX_INACTIVITY)
continue;
struct json_object *obj = json_object_new_object();
json_object_object_add(obj, "signal", json_object_new_int(entry->signal));
json_object_object_add(obj, "noise", json_object_new_int(entry->noise));
json_object_object_add(obj, "inactive", json_object_new_int(entry->inactive));
char mac[18];
snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
entry->mac[0], entry->mac[1], entry->mac[2],
entry->mac[3], entry->mac[4], entry->mac[5]);
json_object_object_add(neighbours, mac, obj);
}
struct json_object *ret = json_object_new_object();
if (json_object_object_length(neighbours))
json_object_object_add(ret, "neighbours", neighbours);
else
json_object_put(neighbours);
return ret;
}
static struct json_object * get_wifi(void) {
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return NULL;
ctx->flags &= ~UCI_FLAG_STRICT;
struct json_object *ret = json_object_new_object();
struct uci_package *p;
if (uci_load(ctx, "wireless", &p))
goto end;
struct uci_element *e;
uci_foreach_element(&p->sections, e) {
struct uci_section *s = uci_to_section(e);
if (strcmp(s->type, "wifi-iface"))
continue;
const char *proto = uci_lookup_option_string(ctx, s, "mode");
if (!proto || strcmp(proto, "mesh"))
continue;
const char *ifname = uci_lookup_option_string(ctx, s, "ifname");
if (!ifname)
continue;
char *ifaddr = gluonutil_get_interface_address(ifname);
if (!ifaddr)
continue;
struct json_object *neighbours = get_wifi_neighbours(ifname);
if (neighbours)
json_object_object_add(ret, ifaddr, neighbours);
free(ifaddr);
}
end:
uci_free_context(ctx);
return ret;
}
struct json_object * respondd_provider_neighbours(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
struct json_object *wifi = get_wifi();
if (wifi)
json_object_object_add(ret, "wifi", wifi);
return ret;
}
/* SPDX-FileCopyrightText: 2016-2019, Matthias Schiffer <mschiffer@universe-factory.net> */
/* SPDX-License-Identifier: BSD-2-Clause */
#include "respondd-common.h"
#include <libgluonutil.h>
#include <libplatforminfo.h>
#include <json-c/json.h>
#include <uci.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static struct json_object * gluon_version(void) {
char *version = gluonutil_read_line("/lib/gluon/gluon-version");
if (!version)
return NULL;
char full_version[6 + strlen(version) + 1];
snprintf(full_version, sizeof(full_version), "gluon-%s", version);
free(version);
return json_object_new_string(full_version);
}
static struct json_object * get_site_code(void) {
struct json_object *site = gluonutil_load_site_config();
if (!site)
return NULL;
struct json_object *ret = NULL;
json_object_object_get_ex(site, "site_code", &ret);
if (ret)
json_object_get(ret);
json_object_put(site);
return ret;
}
static struct json_object * get_primary_domain_code(void) {
return gluonutil_wrap_and_free_string(gluonutil_get_primary_domain());
}
static struct json_object * get_domain_code(void) {
return gluonutil_wrap_and_free_string(gluonutil_get_domain());
}
static struct json_object * get_hostname(void) {
struct json_object *ret = NULL;
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return NULL;
ctx->flags &= ~UCI_FLAG_STRICT;
char section[] = "system.@system[0]";
struct uci_ptr ptr;
if (uci_lookup_ptr(ctx, &ptr, section, true))
goto error;
struct uci_section *s = ptr.s;
const char *hostname = uci_lookup_option_string(ctx, s, "pretty_hostname");
if (!hostname)
hostname = uci_lookup_option_string(ctx, s, "hostname");
ret = gluonutil_wrap_string(hostname);
error:
uci_free_context(ctx);
return ret;
}
struct json_object * respondd_provider_nodeinfo(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
json_object_object_add(ret, "hostname", get_hostname());
struct json_object *hardware = json_object_new_object();
const char *model = platforminfo_get_model();
if (model)
json_object_object_add(hardware, "model", json_object_new_string(model));
json_object_object_add(hardware, "nproc", json_object_new_int(sysconf(_SC_NPROCESSORS_ONLN)));
json_object_object_add(ret, "hardware", hardware);
struct json_object *network = json_object_new_object();
json_object_object_add(network, "mac", gluonutil_wrap_and_free_string(gluonutil_get_sysconfig("primary_mac")));
json_object_object_add(ret, "network", network);
struct json_object *software = json_object_new_object();
struct json_object *software_firmware = json_object_new_object();
json_object_object_add(software_firmware, "base", gluon_version());
json_object_object_add(software_firmware, "release", gluonutil_wrap_and_free_string(gluonutil_read_line("/lib/gluon/release")));
json_object_object_add(software_firmware, "target", gluonutil_wrap_string(platforminfo_get_target()));
json_object_object_add(software_firmware, "subtarget", gluonutil_wrap_string(platforminfo_get_subtarget()));
json_object_object_add(software_firmware, "image_name", gluonutil_wrap_string(platforminfo_get_image_name()));
json_object_object_add(software, "firmware", software_firmware);
json_object_object_add(ret, "software", software);
struct json_object *system = json_object_new_object();
json_object_object_add(system, "site_code", get_site_code());
if (gluonutil_has_domains()) {
json_object_object_add(system, "domain_code", get_domain_code());
json_object_object_add(system, "primary_domain_code", get_primary_domain_code());
}
json_object_object_add(ret, "system", system);
return ret;
}
/* SPDX-FileCopyrightText: 2016-2019, Matthias Schiffer <mschiffer@universe-factory.net> */
/* SPDX-License-Identifier: BSD-2-Clause */
#include "respondd-common.h"
#include <libgluonutil.h>
#include <iwinfo.h>
#include <json-c/json.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/vfs.h>
static void add_uptime(struct json_object *obj) {
FILE *f = fopen("/proc/uptime", "r");
struct json_object* jso;
if (!f)
return;
double uptime, idletime;
if (fscanf(f, "%lf %lf", &uptime, &idletime) == 2) {
jso = json_object_new_double(uptime);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
json_object_object_add(obj, "uptime", jso);
jso = json_object_new_double(idletime);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
json_object_object_add(obj, "idletime", jso);
}
fclose(f);
}
static void add_loadavg(struct json_object *obj) {
FILE *f = fopen("/proc/loadavg", "r");
if (!f)
return;
double loadavg;
unsigned proc_running, proc_total;
if (fscanf(f, "%lf %*f %*f %u/%u", &loadavg, &proc_running, &proc_total) == 3) {
struct json_object *jso = json_object_new_double(loadavg);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
json_object_object_add(obj, "loadavg", jso);
struct json_object *processes = json_object_new_object();
json_object_object_add(processes, "running", json_object_new_int(proc_running));
json_object_object_add(processes, "total", json_object_new_int(proc_total));
json_object_object_add(obj, "processes", processes);
}
fclose(f);
}
static struct json_object * get_memory(void) {
FILE *f = fopen("/proc/meminfo", "r");
if (!f)
return NULL;
struct json_object *ret = json_object_new_object();
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, f) >= 0) {
char label[32];
unsigned value;
if (sscanf(line, "%31[^:]: %u", label, &value) != 2)
continue;
if (!strcmp(label, "MemTotal"))
json_object_object_add(ret, "total", json_object_new_int(value));
else if (!strcmp(label, "MemFree"))
json_object_object_add(ret, "free", json_object_new_int(value));
else if (!strcmp(label, "MemAvailable"))
json_object_object_add(ret, "available", json_object_new_int(value));
else if (!strcmp(label, "Buffers"))
json_object_object_add(ret, "buffers", json_object_new_int(value));
else if (!strcmp(label, "Cached"))
json_object_object_add(ret, "cached", json_object_new_int(value));
}
free(line);
fclose(f);
return ret;
}
static struct json_object * get_stat(void) {
FILE *f = fopen("/proc/stat", "r");
if (!f)
return NULL;
struct json_object *stat = json_object_new_object();
struct json_object *ret = NULL;
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, f) >= 0) {
char label[32];
if (sscanf(line, "%31s", label) != 1){
goto invalid_stat_format;
}
if (!strcmp(label, "cpu")) {
int64_t user, nice, system, idle, iowait, irq, softirq;
if (sscanf(line, "%*s %"SCNd64" %"SCNd64" %"SCNd64" %"SCNd64" %"SCNd64" %"SCNd64" %"SCNd64,
&user, &nice, &system, &idle, &iowait, &irq, &softirq) != 7)
goto invalid_stat_format;
struct json_object *cpu = json_object_new_object();
json_object_object_add(cpu, "user", json_object_new_int64(user));
json_object_object_add(cpu, "nice", json_object_new_int64(nice));
json_object_object_add(cpu, "system", json_object_new_int64(system));
json_object_object_add(cpu, "idle", json_object_new_int64(idle));
json_object_object_add(cpu, "iowait", json_object_new_int64(iowait));
json_object_object_add(cpu, "irq", json_object_new_int64(irq));
json_object_object_add(cpu, "softirq", json_object_new_int64(softirq));
json_object_object_add(stat, "cpu", cpu);
} else if (!strcmp(label, "ctxt")) {
int64_t ctxt;
if (sscanf(line, "%*s %"SCNd64, &ctxt) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "ctxt", json_object_new_int64(ctxt));
} else if (!strcmp(label, "intr")) {
int64_t total_intr;
if (sscanf(line, "%*s %"SCNd64, &total_intr) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "intr", json_object_new_int64(total_intr));
} else if (!strcmp(label, "softirq")) {
int64_t total_softirq;
if (sscanf(line, "%*s %"SCNd64, &total_softirq) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "softirq", json_object_new_int64(total_softirq));
} else if (!strcmp(label, "processes")) {
int64_t processes;
if (sscanf(line, "%*s %"SCNd64, &processes) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "processes", json_object_new_int64(processes));
}
}
ret = stat;
invalid_stat_format:
if (!ret)
json_object_put(stat);
free(line);
fclose(f);
return ret;
}
static struct json_object * get_rootfs_usage(void) {
struct statfs s;
if (statfs("/", &s))
return NULL;
struct json_object *jso = json_object_new_double(1 - (double)s.f_bfree / s.f_blocks);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.4f", NULL);
return jso;
}
static struct json_object * get_time(void) {
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) != 0)
return NULL;
return json_object_new_int64(now.tv_sec);
}
static void count_iface_stations(size_t *wifi24, size_t *wifi5, const char *ifname) {
const struct iwinfo_ops *iw = iwinfo_backend(ifname);
if (!iw)
return;
int freq;
if (iw->frequency(ifname, &freq) < 0)
return;
size_t *wifi;
if (freq >= 2400 && freq < 2500)
wifi = wifi24;
else if (freq >= 5000 && freq < 6000)
wifi = wifi5;
else
return;
int len;
char buf[IWINFO_BUFSIZE];
if (iw->assoclist(ifname, buf, &len) < 0)
return;
struct iwinfo_assoclist_entry *entry;
for (entry = (struct iwinfo_assoclist_entry *)buf; (char*)(entry+1) <= buf + len; entry++) {
if (entry->inactive > MAX_INACTIVITY)
continue;
(*wifi)++;
}
}
static void count_stations(size_t *wifi24, size_t *wifi5, size_t *owe24, size_t *owe5) {
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return;
ctx->flags &= ~UCI_FLAG_STRICT;
struct uci_package *p;
if (uci_load(ctx, "wireless", &p))
goto end;
struct uci_element *e;
uci_foreach_element(&p->sections, e) {
struct uci_section *s = uci_to_section(e);
if (strcmp(s->type, "wifi-iface"))
continue;
const char *network = uci_lookup_option_string(ctx, s, "network");
if (!network || strcmp(network, "client"))
continue;
const char *mode = uci_lookup_option_string(ctx, s, "mode");
if (!mode || strcmp(mode, "ap"))
continue;
const char *ifname = uci_lookup_option_string(ctx, s, "ifname");
if (!ifname)
continue;
if (strstr(ifname, "owe") == ifname)
count_iface_stations(owe24, owe5, ifname);
count_iface_stations(wifi24, wifi5, ifname);
}
end:
uci_free_context(ctx);
}
static struct json_object * get_clients(void) {
size_t wifi24 = 0, wifi5 = 0, owe24 = 0, owe5 = 0;
count_stations(&wifi24, &wifi5, &owe24, &owe5);
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "wifi", json_object_new_int(wifi24 + wifi5));
json_object_object_add(ret, "wifi24", json_object_new_int(wifi24));
json_object_object_add(ret, "wifi5", json_object_new_int(wifi5));
json_object_object_add(ret, "owe", json_object_new_int(owe24 + owe5));
json_object_object_add(ret, "owe24", json_object_new_int(owe24));
json_object_object_add(ret, "owe5", json_object_new_int(owe5));
return ret;
}
struct json_object * respondd_provider_statistics(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
json_object_object_add(ret, "time", get_time());
json_object_object_add(ret, "rootfs_usage", get_rootfs_usage());
json_object_object_add(ret, "memory", get_memory());
json_object_object_add(ret, "stat", get_stat());
json_object_object_add(ret, "clients", get_clients());
add_uptime(ret);
add_loadavg(ret);
return ret;
}
/*
Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* SPDX-FileCopyrightText: 2016-2019, Matthias Schiffer <mschiffer@universe-factory.net> */
/* SPDX-License-Identifier: BSD-2-Clause */
#include "respondd-common.h"
#include <respondd.h>
#include <json-c/json.h>
#include <libgluonutil.h>
#include <libplatforminfo.h>
#include <uci.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>
#include <sys/vfs.h>
static struct json_object * gluon_version(void) {
char *version = gluonutil_read_line("/lib/gluon/gluon-version");
if (!version)
return NULL;
char full_version[6 + strlen(version) + 1];
snprintf(full_version, sizeof(full_version), "gluon-%s", version);
free(version);
return json_object_new_string(full_version);
}
static struct json_object * get_site_code(void) {
struct json_object *site = gluonutil_load_site_config();
if (!site)
return NULL;
struct json_object *ret = NULL;
json_object_object_get_ex(site, "site_code", &ret);
if (ret)
json_object_get(ret);
json_object_put(site);
return ret;
}
static struct json_object * get_domain_code(void) {
return gluonutil_wrap_and_free_string(gluonutil_get_domain());
}
static struct json_object * get_hostname(void) {
struct json_object *ret = NULL;
struct uci_context *ctx = uci_alloc_context();
if (!ctx)
return NULL;
ctx->flags &= ~UCI_FLAG_STRICT;
char section[] = "system.@system[0]";
struct uci_ptr ptr;
if (uci_lookup_ptr(ctx, &ptr, section, true))
goto error;
struct uci_section *s = ptr.s;
const char *hostname = uci_lookup_option_string(ctx, s, "pretty_hostname");
if (!hostname)
hostname = uci_lookup_option_string(ctx, s, "hostname");
ret = gluonutil_wrap_string(hostname);
error:
uci_free_context(ctx);
return ret;
}
static struct json_object * respondd_provider_nodeinfo(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
json_object_object_add(ret, "hostname", get_hostname());
struct json_object *hardware = json_object_new_object();
const char *model = platforminfo_get_model();
if (model)
json_object_object_add(hardware, "model", json_object_new_string(model));
json_object_object_add(hardware, "nproc", json_object_new_int(sysconf(_SC_NPROCESSORS_ONLN)));
json_object_object_add(ret, "hardware", hardware);
struct json_object *network = json_object_new_object();
json_object_object_add(network, "mac", gluonutil_wrap_and_free_string(gluonutil_get_sysconfig("primary_mac")));
json_object_object_add(ret, "network", network);
struct json_object *software = json_object_new_object();
struct json_object *software_firmware = json_object_new_object();
json_object_object_add(software_firmware, "base", gluon_version());
json_object_object_add(software_firmware, "release", gluonutil_wrap_and_free_string(gluonutil_read_line("/lib/gluon/release")));
json_object_object_add(software, "firmware", software_firmware);
json_object_object_add(ret, "software", software);
struct json_object *system = json_object_new_object();
json_object_object_add(system, "site_code", get_site_code());
if (gluonutil_has_domains())
json_object_object_add(system, "domain_code", get_domain_code());
json_object_object_add(ret, "system", system);
return ret;
}
static void add_uptime(struct json_object *obj) {
FILE *f = fopen("/proc/uptime", "r");
struct json_object* jso;
if (!f)
return;
double uptime, idletime;
if (fscanf(f, "%lf %lf", &uptime, &idletime) == 2) {
jso = json_object_new_double(uptime);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
json_object_object_add(obj, "uptime", jso);
jso = json_object_new_double(idletime);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
json_object_object_add(obj, "idletime", jso);
}
fclose(f);
}
static void add_loadavg(struct json_object *obj) {
FILE *f = fopen("/proc/loadavg", "r");
if (!f)
return;
double loadavg;
unsigned proc_running, proc_total;
if (fscanf(f, "%lf %*f %*f %u/%u", &loadavg, &proc_running, &proc_total) == 3) {
struct json_object *jso = json_object_new_double(loadavg);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
json_object_object_add(obj, "loadavg", jso);
struct json_object *processes = json_object_new_object();
json_object_object_add(processes, "running", json_object_new_int(proc_running));
json_object_object_add(processes, "total", json_object_new_int(proc_total));
json_object_object_add(obj, "processes", processes);
}
fclose(f);
}
static struct json_object * get_memory(void) {
FILE *f = fopen("/proc/meminfo", "r");
if (!f)
return NULL;
struct json_object *ret = json_object_new_object();
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, f) >= 0) {
char label[32];
unsigned value;
if (sscanf(line, "%31[^:]: %u", label, &value) != 2)
continue;
if (!strcmp(label, "MemTotal"))
json_object_object_add(ret, "total", json_object_new_int(value));
else if (!strcmp(label, "MemFree"))
json_object_object_add(ret, "free", json_object_new_int(value));
else if (!strcmp(label, "MemAvailable"))
json_object_object_add(ret, "available", json_object_new_int(value));
else if (!strcmp(label, "Buffers"))
json_object_object_add(ret, "buffers", json_object_new_int(value));
else if (!strcmp(label, "Cached"))
json_object_object_add(ret, "cached", json_object_new_int(value));
}
free(line);
fclose(f);
return ret;
}
static struct json_object * get_stat(void) {
FILE *f = fopen("/proc/stat", "r");
if (!f)
return NULL;
struct json_object *stat = json_object_new_object();
struct json_object *ret = NULL;
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, f) >= 0) {
char label[32];
if (sscanf(line, "%31s", label) != 1){
goto invalid_stat_format;
}
if (!strcmp(label, "cpu")) {
unsigned long long user, nice, system, idle, iowait, irq, softirq;
if (sscanf(line, "%*s %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64,
&user, &nice, &system, &idle, &iowait, &irq, &softirq) != 7)
goto invalid_stat_format;
struct json_object *cpu = json_object_new_object();
json_object_object_add(cpu, "user", json_object_new_int64(user));
json_object_object_add(cpu, "nice", json_object_new_int64(nice));
json_object_object_add(cpu, "system", json_object_new_int64(system));
json_object_object_add(cpu, "idle", json_object_new_int64(idle));
json_object_object_add(cpu, "iowait", json_object_new_int64(iowait));
json_object_object_add(cpu, "irq", json_object_new_int64(irq));
json_object_object_add(cpu, "softirq", json_object_new_int64(softirq));
json_object_object_add(stat, "cpu", cpu);
} else if (!strcmp(label, "ctxt")) {
unsigned long long ctxt;
if (sscanf(line, "%*s %"SCNu64, &ctxt) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "ctxt", json_object_new_int64(ctxt));
} else if (!strcmp(label, "intr")) {
unsigned long long total_intr;
if (sscanf(line, "%*s %"SCNu64, &total_intr) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "intr", json_object_new_int64(total_intr));
} else if (!strcmp(label, "softirq")) {
unsigned long long total_softirq;
if (sscanf(line, "%*s %"SCNu64, &total_softirq) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "softirq", json_object_new_int64(total_softirq));
} else if (!strcmp(label, "processes")) {
unsigned long long processes;
if (sscanf(line, "%*s %"SCNu64, &processes) != 1)
goto invalid_stat_format;
json_object_object_add(stat, "processes", json_object_new_int64(processes));
}
}
ret = stat;
invalid_stat_format:
if (!ret)
json_object_put(stat);
free(line);
fclose(f);
return ret;
}
static struct json_object * get_rootfs_usage(void) {
struct statfs s;
if (statfs("/", &s))
return NULL;
struct json_object *jso = json_object_new_double(1 - (double)s.f_bfree / s.f_blocks);
json_object_set_serializer(jso, json_object_double_to_json_string, "%.4f", NULL);
return jso;
}
static struct json_object * get_time(void) {
struct timespec now;
if (clock_gettime(CLOCK_REALTIME, &now) != 0)
return NULL;
return json_object_new_int64(now.tv_sec);
}
static struct json_object * respondd_provider_statistics(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
json_object *time = get_time();
if (time != NULL)
json_object_object_add(ret, "time", time);
json_object_object_add(ret, "rootfs_usage", get_rootfs_usage());
json_object_object_add(ret, "memory", get_memory());
json_object_object_add(ret, "stat", get_stat());
add_uptime(ret);
add_loadavg(ret);
return ret;
}
static struct json_object * respondd_provider_neighbours(void) {
struct json_object *ret = json_object_new_object();
json_object_object_add(ret, "node_id", gluonutil_wrap_and_free_string(gluonutil_get_node_id()));
return ret;
}
__attribute__ ((visibility ("default")))
const struct respondd_provider_info respondd_providers[] = {
{"nodeinfo", respondd_provider_nodeinfo},
{"statistics", respondd_provider_statistics},
......
include $(TOPDIR)/rules.mk
PKG_NAME:=gluon-scheduled-domain-switch
include ../gluon.mk
define Package/gluon-scheduled-domain-switch
TITLE:=Allows scheduled migrations between domains
DEPENDS:=+gluon-core @GLUON_MULTIDOMAIN
endef
$(eval $(call BuildPackageGluon,gluon-scheduled-domain-switch))
if need_table(in_domain({'domain_switch'}), nil, false) then
need_domain_name(in_domain({'domain_switch', 'target_domain'}))
need_number(in_domain({'domain_switch', 'switch_after_offline_mins'}))
need_number(in_domain({'domain_switch', 'switch_time'}))
need_string_array_match(in_domain({'domain_switch', 'connection_check_targets'}), '^[%x:]+$')
end
#!/usr/bin/lua
local uci = require('simple-uci').cursor()
local unistd = require 'posix.unistd'
local util = require 'gluon.util'
local site = require 'gluon.site'
-- Returns true if node was offline long enough to perform domain switch
local function switch_after_min_reached()
if not unistd.access("/tmp/gluon_offline") then
return false
end
local switch_after_sec = site.domain_switch.switch_after_offline_mins() * 60
local current_uptime = util.get_uptime()
if current_uptime == nil then
return false
end
local f = util.readfile("/tmp/gluon_offline")
if f == nil then
return false
end
local offline_since = tonumber(f)
local offline_time_sec = current_uptime - offline_since
if offline_time_sec > switch_after_sec then
return true
end
return false
end
-- Returns true in case switch time has passed
local function switch_time_passed()
local current_time = os.time()
local switch_time = site.domain_switch.switch_time()
return switch_time < current_time
end
if site.domain_switch() == nil then
-- Switch not applicable for current domain
print("No domain switch defined for the current domain.")
os.exit(0)
end
local current_domain = uci:get("gluon", "core", "domain")
local target_domain = site.domain_switch.target_domain()
if target_domain == current_domain then
-- Current and target domain are equal
print("Domain '" .. target_domain .. "' equals current domain.")
os.exit(1)
end
if not switch_after_min_reached() and not switch_time_passed() then
-- Neither switch-time passed nor switch_after_min reached
os.exit(0)
end
local cmd = {[0] = 'gluon-switch-domain', target_domain}
unistd.execp(cmd[0], cmd)
#!/usr/bin/lua
local site = require 'gluon.site'
local cronfile = "/usr/lib/micron.d/gluon-scheduled-domain-switch"
-- Check if domain switch is scheduled
if site.domain_switch() == nil then
-- In case no domain switch is scheduled, remove cronfile
os.remove(cronfile)
os.exit(0)
end
-- Only in case domain switch is scheduled
local f = io.open(cronfile, "w")
f:write("* * * * * /usr/bin/gluon-check-connection\n")
f:write("*/5 * * * * /lib/gluon/scheduled-domain-switch/switch-domain\n")
f:close()
#!/usr/bin/lua
local unistd = require 'posix.unistd'
local util = require 'gluon.util'
local site = require 'gluon.site'
local offline_flag_file = "/tmp/gluon_offline"
local is_offline = true
-- Check if domain-switch is scheduled
if site.domain_switch() == nil then
-- Switch not applicable for current domain
os.exit(0)
end
-- Check reachability of pre-defined targets
for _, ip in ipairs(site.domain_switch.connection_check_targets()) do
local exit_code = os.execute("ping -c 1 -w 10 " .. ip)
if exit_code == 0 then
is_offline = false
break
end
end
if is_offline then
-- Check if we were previously offline
if unistd.access(offline_flag_file) then
os.exit(0)
end
-- Create offline flag
local f = io.open(offline_flag_file, "w")
f:write(tostring(util.get_uptime()))
f:close()
else
os.remove(offline_flag_file)
end
......@@ -4,17 +4,42 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=gluon-setup-mode
PKG_VERSION:=1
include ../gluon.mk
define Package/gluon-setup-mode
TITLE:=Setup mode
DEPENDS:=+gluon-core +gluon-lock-password +ubus +dnsmasq
DEPENDS:=+gluon-core +gluon-lock-password +ubus +dnsmasq-full
endef
define Package/gluon-setup-mode/description
Offline mode to perform basic setup in a secure manner.
endef
define Package/gluon-setup-mode/conffiles
/etc/config/gluon-setup-mode
endef
init_links := \
K89log \
K98boot \
K99umount \
S00sysfixtime \
S10boot \
S10gluon-core-reconfigure \
S10system \
S11sysctl \
S12log \
S95done
define Package/gluon-setup-mode/install
$(Gluon/Build/Install)
$(LN) S20network $(1)/lib/gluon/setup-mode/rc.d/K90network
for link in $(init_links); do \
$(LN) "/etc/init.d/$$$${link:3}" "$(1)/lib/gluon/setup-mode/rc.d/$$$${link}"; \
done
endef
$(eval $(call BuildPackageGluon,gluon-setup-mode))
......@@ -6,13 +6,17 @@ wait=3
wait_setup_mode() {
sleep $wait
uci set 'gluon-setup-mode.@setup_mode[0].enabled=1'
uci commit gluon-setup-mode
reboot
gluon-enter-setup-mode
}
# Special case for devices that use capacitive buttons
case "$(board_name)" in
genexis,pulse-ex400)
[ "$BUTTON" = wps ] && exit 0
;;
esac
if [ "$BUTTON" = wps -o "$BUTTON" = reset ]; then
if [ "$BUTTON" = wps ] || [ "$BUTTON" = reset ] || [ "$BUTTON" = phone ]; then
case "$ACTION" in
pressed)
wait_setup_mode &
......@@ -21,7 +25,7 @@ if [ "$BUTTON" = wps -o "$BUTTON" = reset ]; then
;;
released)
if [ -r /tmp/.wait_setup_mode ]; then
kill $(cat /tmp/.wait_setup_mode)
kill "$(cat /tmp/.wait_setup_mode)"
rm /tmp/.wait_setup_mode
fi
;;
......
/etc/init.d/log
\ No newline at end of file
S20network
\ No newline at end of file
/etc/init.d/boot
\ No newline at end of file
/etc/init.d/umount
\ No newline at end of file
/etc/init.d/sysfixtime
\ No newline at end of file
/etc/init.d/boot
\ No newline at end of file