From 78e8b7e4917f3f96ab7be31215fd77f2dfe7c3d1 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Sun, 24 Dec 2023 00:35:41 +0300 Subject: [PATCH] The code is rewritten from scratch, based on udev --- .vscode/settings.json | 11 +- .vscode/tasks.json | 2 +- CHANGELOG.md | 6 +- CMakeLists.txt | 10 +- README.md | 32 +-- services/mportlink.service | 2 +- src/mm.c | 41 ---- src/mm.h | 11 - src/mportlink-core.c | 470 +++++++++++++++++++++---------------- src/mportlink-core.h | 18 ++ src/mportlink-lib.c | 8 + src/mportlink-lib.h | 13 + src/mportlink.c | 91 +------ src/mportlink.h | 17 +- 14 files changed, 356 insertions(+), 376 deletions(-) delete mode 100644 src/mm.c delete mode 100644 src/mm.h create mode 100644 src/mportlink-core.h create mode 100644 src/mportlink-lib.c create mode 100644 src/mportlink-lib.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 344ca2e..1dba064 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,7 +19,14 @@ "cstdint": "c", "fcntl.h": "c", "file.h": "c", - "dir.h": "c" + "dir.h": "c", + "mportlink-core.h": "c", + "mportlink.h": "c", + "mportlink-lib.h": "c", + "errno.h": "c" }, - "cmake.configureOnOpen": false + "cmake.configureOnOpen": false, + "editor.rulers": [ + 80 + ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f271081..07c5bb6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -8,7 +8,7 @@ "-fdiagnostics-color=always", "-g", "*.c", - "`pkg-config", "--libs", "--cflags", "mm-glib`", + "`pkg-config", "--libs", "udev`", "-o", "${workspaceFolder}/bin/mportlink" ], diff --git a/CHANGELOG.md b/CHANGELOG.md index 373bcf6..552e26b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## [0.2.0](https://github.com/AlexanderZhirov/MPortLink/compare/0.1.0...v0.2.0) +## [Unreleased] +### Changed +- The code is rewritten from scratch, based on `udev` + +## [0.2.0](https://github.com/AlexanderZhirov/MPortLink/compare/0.1.0...0.2.0) ### Changed - Creation of links to ports has been moved to the directory `/dev/dongle` diff --git a/CMakeLists.txt b/CMakeLists.txt index 52b53fd..992027f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,11 +5,11 @@ file(GLOB_RECURSE SRC_FILES_LIST src/*.c) find_package(PkgConfig REQUIRED) -pkg_search_module(MM REQUIRED mm-glib) +pkg_search_module(UDEV REQUIRED udev) -include_directories(${MM_INCLUDE_DIRS}) -link_directories(${MM_LIBRARY_DIRS}) -add_definitions(${MM_CFLAGS_OTHER}) +include_directories(${UDEV_INCLUDE_DIRS}) +link_directories(${UDEV_LIBRARY_DIRS}) +add_definitions(${UDEV_CFLAGS_OTHER}) add_executable(${PROJECT_NAME} ${SRC_FILES_LIST}) -target_link_libraries(${PROJECT_NAME} ${MM_LIBRARIES}) +target_link_libraries(${PROJECT_NAME} ${UDEV_LIBRARIES}) diff --git a/README.md b/README.md index fcde2ff..713db61 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,24 @@ # MPortLink +[![license](https://img.shields.io/github/license/AlexanderZhirov/mportlink.svg?sort=semver&style=for-the-badge&color=green)](https://www.gnu.org/licenses/old-licenses/gpl-2.0.html) +[![main](https://img.shields.io/badge/dynamic/json.svg?label=git.zhirov.kz&style=for-the-badge&url=https://git.zhirov.kz/api/v1/repos/alexander/MPortLink/tags&query=$[0].name&color=violet&logo=C)](https://git.zhirov.kz/alexander/MPortLink) +[![githab](https://img.shields.io/github/v/tag/AlexanderZhirov/mportlink.svg?sort=semver&style=for-the-badge&color=blue&label=github&logo=C)](https://github.com/AlexanderZhirov/mportlink) +[![linux](https://img.shields.io/badge/Linux-FCC624?style=for-the-badge&logo=linux&logoColor=black)](https://www.linux.org/) + **MPortLink** (*modem port link*) is designed to track connected USB modems and create permanent symbolic links to ports. MPortLink was developed for direct use of several USB modems when connected to Asterisk telephony, the ports of which were randomly determined by the operating system. The utility allows you to create permanent symbolic links to devices, regardless of the order in which the ports of the USB modem device were defined. -Based on [ModemManager](https://gitlab.freedesktop.org/mobile-broadband/ModemManager). - -## Dependency - -The installed `modemmanager` is required for use. - ## Build -`libmm-glib` library is required for the build. It is called differently in different distributions, for example: `libmm-glib-dev` or `libmm-glib-devel`. - -After installing the dependencies, you just need to run the script `build.sh `and `mportlink` will be built in the `build` project directory. +Just need to run the script `build.sh `and `mportlink` will be built in the `build` project directory. ### Manually ``` -gcc -Werror -Wall -Os src/*.c `pkg-config --libs --cflags mm-glib` -o mportlink +gcc -Werror -Wall -Os src/*.c `pkg-config --libs libudev` -o mportlink ``` ## Using Superuser rights are required to work. Start the daemon and connect your USB modem. In `journalctl` you will see his work. Links to the ports of the connected modem will be created in the `/dev/dongle` directory. - -``` -Dec 21 13:51:02 solus mportlink[17764]: main: starting the mportlink daemon -Dec 21 13:51:26 solus mportlink[17764]: mpl_symlink_ports, dst_path: the symbolic link has been successfully created [/dev/dongle/358**********26-0 -> /dev/ttyUSB0] -Dec 21 13:51:26 solus mportlink[17764]: mpl_symlink_ports, dst_path: the symbolic link has been successfully created [/dev/dongle/358**********26-1 -> /dev/ttyUSB1] -Dec 21 13:51:41 solus mportlink[17764]: mpl_unlink_ports, dst_path: link was successfully deleted [/dev/dongle/358**********26-0] -Dec 21 13:51:41 solus mportlink[17764]: mpl_unlink_ports, dst_path: link was successfully deleted [/dev/dongle/358**********26-1] -Dec 21 13:51:48 solus mportlink[17764]: signals_handler: cancelling the operation... -Dec 21 13:51:48 solus mportlink[17764]: signals_handler: cancelling the main loop... -Dec 21 13:51:48 solus mportlink[17764]: main: stopping the mportlink daemon -``` - -## To-Do - -Currently, the utility is linked to the ModemManager server, through which the connected modems are identified. It is planned to disconnect from this server, as there is a problem with the capture of the device and the unavailability of using Asterisk. diff --git a/services/mportlink.service b/services/mportlink.service index 88cfd8e..a037064 100644 --- a/services/mportlink.service +++ b/services/mportlink.service @@ -1,6 +1,6 @@ [Unit] Description=Modem Port Link -After=ModemManager.service +After= [Service] ExecStart=/usr/bin/mportlink diff --git a/src/mm.c b/src/mm.c deleted file mode 100644 index d2629fd..0000000 --- a/src/mm.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "mportlink.h" - -MMManager *mmcli_get_manager_finish(GAsyncResult *res) -{ - return g_task_propagate_pointer(G_TASK(res), NULL); -} - -void manager_new_ready(GDBusConnection *connection, GAsyncResult *res, GTask *task) -{ - MMManager *manager; - gchar *name_owner; - GError *error = NULL; - - manager = mm_manager_new_finish(res, &error); - if (!manager) - { - syslog(LOG_ERR, "manager_new_ready, manager: couldn't create manager: %s\n", error ? error->message : "unknown error"); - exit(EXIT_FAILURE); - } - - name_owner = g_dbus_object_manager_client_get_name_owner(G_DBUS_OBJECT_MANAGER_CLIENT(manager)); - if (!name_owner) - { - syslog(LOG_ERR, "manager_new_ready, name_owner: couldn't find the ModemManager process in the bus"); - exit(EXIT_FAILURE); - } - - g_free(name_owner); - - g_task_return_pointer(task, manager, g_object_unref); - g_object_unref(task); -} - -void mmcli_get_manager(GDBusConnection *connection, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) -{ - GTask *task; - - task = g_task_new(connection, cancellable, callback, user_data); - - mm_manager_new(connection, G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_DO_NOT_AUTO_START, cancellable, (GAsyncReadyCallback)manager_new_ready, task); -} diff --git a/src/mm.h b/src/mm.h deleted file mode 100644 index 19a61eb..0000000 --- a/src/mm.h +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -typedef struct -{ - MMManager *manager; - GCancellable *cancellable; -} Context; - -MMManager *mmcli_get_manager_finish(GAsyncResult *res); -void mmcli_get_manager(GDBusConnection *connection, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); diff --git a/src/mportlink-core.c b/src/mportlink-core.c index a1b3732..bff39bc 100644 --- a/src/mportlink-core.c +++ b/src/mportlink-core.c @@ -1,253 +1,317 @@ -#include "mportlink.h" -#include -#include -#include +/* + * mportlink-core.c + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#include #include +#include +#include +#include +#include +#include +#include "mportlink-lib.h" +#include "mportlink-core.h" + +#define LOCKFILE "/run/lock/mportlink.lock" #define DEV_PATH "/dev/" -#define DONGLE_PATH "/dev/dongle/" +#define DONGLE_PATH (DEV_PATH "dongle/") -typedef struct +#define MPLOG(PRIORITY, fmt, ...) do { \ + char buf[256]; \ + snprintf(buf, sizeof(buf), fmt __VA_OPT__(,) __VA_ARGS__); \ + PRIORITY < LOG_WARNING ? \ + syslog(PRIORITY, "%s(): %s (%s)", __func__, buf, \ + strerror(errno)) : \ + syslog(PRIORITY, "%s(): %s", __func__, buf); \ + } while(0) + +static void signals_handler(int signum) { - const gchar *manufacturer; - const gchar *model; - const gchar *imei; - const gchar *device_identifier; - MMModemPortInfo *ports; - guint n_ports; -} MPLmodem; + MPLOG(LOG_WARNING, "cancelling the main loop (%d)", signum); +} void mpl_create_dongle_dir() { struct stat st; - if (stat(DONGLE_PATH, &st) == 0) - { - if (!S_ISDIR(st.st_mode)) - { - syslog(LOG_ERR, "mpl_create_dongle_dir: %s exists and is not a directory", DONGLE_PATH); - raise(SIGINT); + if (stat(DONGLE_PATH, &st) == 0) { + if (!S_ISDIR(st.st_mode)) { + MPLOG(LOG_WARNING, "%s exists and is not a directory", + DONGLE_PATH); + exit(EXIT_FAILURE); } - } - else if (mkdir(DONGLE_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) - { - syslog(LOG_ERR, "mpl_create_dongle_dir: unable to create a directory %s", DONGLE_PATH); - raise(SIGINT); + } else if (mkdir(DONGLE_PATH, + S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) { + MPLOG(LOG_ERR, "unable to create a directory %s", DONGLE_PATH); + exit(EXIT_FAILURE); } } -MPLmodem *mpl_get_new_modem() +int mpl_init() { - MPLmodem *modem = (MPLmodem *)calloc(1, sizeof(MPLmodem)); + openlog(MPL, LOG_PID, LOG_SYSLOG); - if (modem == NULL) - { - syslog(LOG_ERR, "mpl_get_new_modem, modem: memory allocation error"); - raise(SIGINT); + int lock_fd = open(LOCKFILE, O_CREAT | O_RDWR, 0644); + if (lock_fd < 0) { + MPLOG(LOG_ERR, "Unable to create a lockfile"); + exit(EXIT_FAILURE); } - return modem; -} - -void mpl_free_modem(MPLmodem *modem) -{ - if (modem->n_ports && modem->ports) - mm_modem_port_info_array_free(modem->ports, modem->n_ports); - free(modem); -} - -void mpl_print_modem_info(MPLmodem *modem, bool connected) -{ - gchar *status; - status = connected ? "Connected" : "Disconnected"; - - g_print("%s %s %s (%s)\n", status, modem->manufacturer, modem->model, modem->device_identifier); - g_print("`-- Count ports: %d\n", modem->n_ports); - - for (guint i = 0; i < modem->n_ports; i++) - { - if (i + 1 < modem->n_ports) - { - g_print(" |-- Port %d:\n", i); - g_print(" | |-- Name: %s\n", modem->ports[i].name); - g_print(" | `-- Link: %s-%d\n", modem->imei, i); - } - else - { - g_print(" `-- Port %d:\n", i); - g_print(" |-- Name: %s\n", modem->ports[i].name); - g_print(" `-- Link: %s-%d\n", modem->imei, i); - } - } -} - -MPLmodem *mpl_get_new_modem_info(MMObject *obj) -{ - MPLmodem *mpl_modem = NULL; - MMModem *mm_modem = mm_object_get_modem(obj); - - if (mm_modem == NULL) - { - syslog(LOG_ERR, "mpl_get_new_modem_info, mm_modem: modem does not implement the interface"); - return NULL; + int rc = flock(lock_fd, LOCK_EX | LOCK_NB); + if (rc || errno == EWOULDBLOCK) { + MPLOG(LOG_ERR, "Only one instance of %s is allowed", MPL); + exit(EXIT_FAILURE); } - mpl_modem = mpl_get_new_modem(); + mpl_create_dongle_dir(); - mpl_modem->manufacturer = mm_modem_get_manufacturer(mm_modem); - mpl_modem->model = mm_modem_get_model(mm_modem); - mpl_modem->imei = mm_modem_get_equipment_identifier(mm_modem); - mpl_modem->device_identifier = mm_modem_get_device_identifier(mm_modem); + MPLOG(LOG_NOTICE, "Starting the %s daemon", MPL); - gboolean success = mm_modem_get_ports(mm_modem, &mpl_modem->ports, &mpl_modem->n_ports); + signal(SIGINT, signals_handler); + signal(SIGHUP, signals_handler); + signal(SIGTERM, signals_handler); - g_object_unref(mm_modem); - - if (success) - return mpl_modem; - else - syslog(LOG_WARNING, "mpl_get_new_modem_info, success: error receiving modem ports"); - - mpl_free_modem(mpl_modem); - - return NULL; + return lock_fd; } -char *mpl_get_dst_path(guint index, const gchar *imei) +int mpl_stop(const int lock_fd) { - char *dst_path = NULL; + flock(lock_fd, LOCK_UN); - guint size_id = snprintf(NULL, 0, "-%d", index); - char *id = (char *)calloc(size_id + 1, sizeof(char)); - - if (id == NULL) - { - syslog(LOG_ERR, "mpl_get_dst_path, id: memory allocation error"); - raise(SIGINT); - } - - sprintf(id, "-%d", index); - - guint dst_path_size = strlen(DONGLE_PATH) + strlen(imei) + size_id; - dst_path = (char *)calloc(dst_path_size + 1, sizeof(char)); - - if (dst_path == NULL) - { - syslog(LOG_ERR, "mpl_get_dst_path, dst_path: memory allocation error"); - raise(SIGINT); - } - - strcpy(dst_path, DONGLE_PATH); - strcat(dst_path, imei); - strcat(dst_path, id); - - free(id); - - return dst_path; + MPLOG(LOG_NOTICE, "Stopping the %s daemon", MPL); + closelog(); } -char *mpl_get_src_path(const gchar *port_name) -{ - char *src_path = NULL; - guint src_path_size = strlen(DEV_PATH) + strlen(port_name); - src_path = (char *)calloc(src_path_size + 1, sizeof(char)); - if (src_path == NULL) - { - syslog(LOG_ERR, "mpl_get_src_path, src_path: memory allocation error"); - raise(SIGINT); - } - strcpy(src_path, DEV_PATH); - strcat(src_path, port_name); +// typedef struct +// { +// const gchar *manufacturer; +// const gchar *model; +// const gchar *imei; +// const gchar *device_identifier; +// MMModemPortInfo *ports; +// guint n_ports; +// } MPLmodem; - return src_path; -} -void mpl_symlink_ports(MPLmodem *modem) -{ - for (guint i = 0; i < modem->n_ports; i++) - { - char *src_path = mpl_get_src_path(modem->ports[i].name); - char *dst_path = mpl_get_dst_path(i, modem->imei); - struct stat st; +// MPLmodem *mpl_get_new_modem() +// { +// MPLmodem *modem = (MPLmodem *)calloc(1, sizeof(MPLmodem)); - if (access(src_path, F_OK) == -1) - { - syslog(LOG_ERR, "mpl_symlink_ports, src_path: device file is not available [%s]", src_path); - free(src_path); - free(dst_path); - continue; - } +// if (modem == NULL) +// { +// syslog(LOG_ERR, "mpl_get_new_modem, modem: memory allocation error"); +// raise(SIGINT); +// } - if (lstat(dst_path, &st) == 0) - { - syslog(LOG_WARNING, "mpl_symlink_ports, dst_path: a symbolic link to the device already exists [%s]", dst_path); - if (unlink(dst_path) == -1) - { - syslog(LOG_ERR, "mpl_symlink_ports, dst_path: link could not be deleted [%s]", dst_path); - free(src_path); - free(dst_path); - continue; - } - } +// return modem; +// } - if (symlink(src_path, dst_path) == 0) - syslog(LOG_NOTICE, "mpl_symlink_ports, dst_path: the symbolic link has been successfully created [%s -> %s]", dst_path, src_path); - else - syslog(LOG_ERR, "mpl_symlink_ports, dst_path: error creating a symbolic link [%s -> %s]", dst_path, src_path); +// void mpl_free_modem(MPLmodem *modem) +// { +// if (modem->n_ports && modem->ports) +// mm_modem_port_info_array_free(modem->ports, modem->n_ports); +// free(modem); +// } - free(src_path); - free(dst_path); - } -} +// void mpl_print_modem_info(MPLmodem *modem, bool connected) +// { +// gchar *status; +// status = connected ? "Connected" : "Disconnected"; -void mpl_unlink_ports(MPLmodem *modem) -{ - for (guint i = 0; i < modem->n_ports; i++) - { - char *dst_path = mpl_get_dst_path(i, modem->imei); - struct stat st; +// g_print("%s %s %s (%s)\n", status, modem->manufacturer, modem->model, modem->device_identifier); +// g_print("`-- Count ports: %d\n", modem->n_ports); - if (lstat(dst_path, &st) == -1) - { - syslog(LOG_WARNING, "mpl_unlink_ports, dst_path: a symbolic link to the device not exists [%s]", dst_path); - free(dst_path); - continue; - } +// for (guint i = 0; i < modem->n_ports; i++) +// { +// if (i + 1 < modem->n_ports) +// { +// g_print(" |-- Port %d:\n", i); +// g_print(" | |-- Name: %s\n", modem->ports[i].name); +// g_print(" | `-- Link: %s-%d\n", modem->imei, i); +// } +// else +// { +// g_print(" `-- Port %d:\n", i); +// g_print(" |-- Name: %s\n", modem->ports[i].name); +// g_print(" `-- Link: %s-%d\n", modem->imei, i); +// } +// } +// } - if (unlink(dst_path) == 0) - syslog(LOG_NOTICE, "mpl_unlink_ports, dst_path: link was successfully deleted [%s]", dst_path); - else - syslog(LOG_ERR, "mpl_unlink_ports, dst_path: link could not be deleted [%s]", dst_path); +// MPLmodem *mpl_get_new_modem_info(MMObject *obj) +// { +// MPLmodem *mpl_modem = NULL; +// MMModem *mm_modem = mm_object_get_modem(obj); - free(dst_path); - } -} +// if (mm_modem == NULL) +// { +// syslog(LOG_ERR, "mpl_get_new_modem_info, mm_modem: modem does not implement the interface"); +// return NULL; +// } -void mpl_device_added(MMManager *manager, MMObject *obj) -{ - MPLmodem *modem = mpl_get_new_modem_info(obj); +// mpl_modem = mpl_get_new_modem(); - if (modem == NULL) - return; +// mpl_modem->manufacturer = mm_modem_get_manufacturer(mm_modem); +// mpl_modem->model = mm_modem_get_model(mm_modem); +// mpl_modem->imei = mm_modem_get_equipment_identifier(mm_modem); +// mpl_modem->device_identifier = mm_modem_get_device_identifier(mm_modem); - // mpl_print_modem_info(modem, true); - mpl_symlink_ports(modem); +// gboolean success = mm_modem_get_ports(mm_modem, &mpl_modem->ports, &mpl_modem->n_ports); - mpl_free_modem(modem); -} +// g_object_unref(mm_modem); -void mpl_device_removed(MMManager *manager, MMObject *obj) -{ - MPLmodem *modem = mpl_get_new_modem_info(obj); +// if (success) +// return mpl_modem; +// else +// syslog(LOG_WARNING, "mpl_get_new_modem_info, success: error receiving modem ports"); - if (modem == NULL) - return; +// mpl_free_modem(mpl_modem); - // mpl_print_modem_info(modem, false); - mpl_unlink_ports(modem); +// return NULL; +// } - mpl_free_modem(modem); -} +// char *mpl_get_dst_path(guint index, const gchar *imei) +// { +// char *dst_path = NULL; + +// guint size_id = snprintf(NULL, 0, "-%d", index); +// char *id = (char *)calloc(size_id + 1, sizeof(char)); + +// if (id == NULL) +// { +// syslog(LOG_ERR, "mpl_get_dst_path, id: memory allocation error"); +// raise(SIGINT); +// } + +// sprintf(id, "-%d", index); + +// guint dst_path_size = strlen(DONGLE_PATH) + strlen(imei) + size_id; +// dst_path = (char *)calloc(dst_path_size + 1, sizeof(char)); + +// if (dst_path == NULL) +// { +// syslog(LOG_ERR, "mpl_get_dst_path, dst_path: memory allocation error"); +// raise(SIGINT); +// } + +// strcpy(dst_path, DONGLE_PATH); +// strcat(dst_path, imei); +// strcat(dst_path, id); + +// free(id); + +// return dst_path; +// } + +// char *mpl_get_src_path(const gchar *port_name) +// { +// char *src_path = NULL; + +// guint src_path_size = strlen(DEV_PATH) + strlen(port_name); +// src_path = (char *)calloc(src_path_size + 1, sizeof(char)); + +// if (src_path == NULL) +// { +// syslog(LOG_ERR, "mpl_get_src_path, src_path: memory allocation error"); +// raise(SIGINT); +// } + +// strcpy(src_path, DEV_PATH); +// strcat(src_path, port_name); + +// return src_path; +// } + +// void mpl_symlink_ports(MPLmodem *modem) +// { +// for (guint i = 0; i < modem->n_ports; i++) +// { +// char *src_path = mpl_get_src_path(modem->ports[i].name); +// char *dst_path = mpl_get_dst_path(i, modem->imei); +// struct stat st; + +// if (access(src_path, F_OK) == -1) +// { +// syslog(LOG_ERR, "mpl_symlink_ports, src_path: device file is not available [%s]", src_path); +// free(src_path); +// free(dst_path); +// continue; +// } + +// if (lstat(dst_path, &st) == 0) +// { +// syslog(LOG_WARNING, "mpl_symlink_ports, dst_path: a symbolic link to the device already exists [%s]", dst_path); +// if (unlink(dst_path) == -1) +// { +// syslog(LOG_ERR, "mpl_symlink_ports, dst_path: link could not be deleted [%s]", dst_path); +// free(src_path); +// free(dst_path); +// continue; +// } +// } + +// if (symlink(src_path, dst_path) == 0) +// syslog(LOG_NOTICE, "mpl_symlink_ports, dst_path: the symbolic link has been successfully created [%s -> %s]", dst_path, src_path); +// else +// syslog(LOG_ERR, "mpl_symlink_ports, dst_path: error creating a symbolic link [%s -> %s]", dst_path, src_path); + +// free(src_path); +// free(dst_path); +// } +// } + +// void mpl_unlink_ports(MPLmodem *modem) +// { +// for (guint i = 0; i < modem->n_ports; i++) +// { +// char *dst_path = mpl_get_dst_path(i, modem->imei); +// struct stat st; + +// if (lstat(dst_path, &st) == -1) +// { +// syslog(LOG_WARNING, "mpl_unlink_ports, dst_path: a symbolic link to the device not exists [%s]", dst_path); +// free(dst_path); +// continue; +// } + +// if (unlink(dst_path) == 0) +// syslog(LOG_NOTICE, "mpl_unlink_ports, dst_path: link was successfully deleted [%s]", dst_path); +// else +// syslog(LOG_ERR, "mpl_unlink_ports, dst_path: link could not be deleted [%s]", dst_path); + +// free(dst_path); +// } +// } + +// void mpl_device_added(MMManager *manager, MMObject *obj) +// { +// MPLmodem *modem = mpl_get_new_modem_info(obj); + +// if (modem == NULL) +// return; + +// // mpl_print_modem_info(modem, true); +// mpl_symlink_ports(modem); + +// mpl_free_modem(modem); +// } + +// void mpl_device_removed(MMManager *manager, MMObject *obj) +// { +// MPLmodem *modem = mpl_get_new_modem_info(obj); + +// if (modem == NULL) +// return; + +// // mpl_print_modem_info(modem, false); +// mpl_unlink_ports(modem); + +// mpl_free_modem(modem); +// } diff --git a/src/mportlink-core.h b/src/mportlink-core.h new file mode 100644 index 0000000..e6f8956 --- /dev/null +++ b/src/mportlink-core.h @@ -0,0 +1,18 @@ +/* + * mportlink-core.h + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#ifndef MPL_CORE_H_ +#define MPL_CORE_H_ + +#include + +#define MPL "mportlink" + +int mpl_init(); +int mpl_stop(const int lock_fd); + +#endif diff --git a/src/mportlink-lib.c b/src/mportlink-lib.c new file mode 100644 index 0000000..2c588b8 --- /dev/null +++ b/src/mportlink-lib.c @@ -0,0 +1,8 @@ +/* + * mportlink-lib.c + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#include "mportlink-lib.h" diff --git a/src/mportlink-lib.h b/src/mportlink-lib.h new file mode 100644 index 0000000..a893af3 --- /dev/null +++ b/src/mportlink-lib.h @@ -0,0 +1,13 @@ +/* + * mportlink-lib.h + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#ifndef MPL_LIB_H_ +#define MPL_LIB_H_ + + + +#endif diff --git a/src/mportlink.c b/src/mportlink.c index 21d66ff..da9d0bd 100644 --- a/src/mportlink.c +++ b/src/mportlink.c @@ -1,90 +1,17 @@ +/* + * mportlink.c + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + #include "mportlink.h" -#include -#include - -#define LOCKFILE "/run/lock/mportlink.lock" -#define MPL "mportlink" - -static Context *ctx; -static GCancellable *cancellable; -static GMainLoop *loop; - -static void signals_handler(int signum) -{ - if (cancellable) - { - if (!g_cancellable_is_cancelled(cancellable)) - { - syslog(LOG_WARNING, "signals_handler: cancelling the operation..."); - g_cancellable_cancel(cancellable); - } - } - - if (loop && g_main_loop_is_running(loop)) - { - syslog(LOG_WARNING, "signals_handler: cancelling the main loop..."); - g_main_loop_quit(loop); - } -} - -static void get_manager_ready(GObject *source, GAsyncResult *result, gpointer none) -{ - ctx->manager = mmcli_get_manager_finish(result); - - g_signal_connect(ctx->manager, "object-added", G_CALLBACK(mpl_device_added), NULL); - g_signal_connect(ctx->manager, "object-removed", G_CALLBACK(mpl_device_removed), NULL); -} int main() { - openlog(MPL, LOG_PID, LOG_SYSLOG); + int lock_fd = mpl_init(); - int lock_fd = open(LOCKFILE, O_CREAT | O_RDWR, 0666); - int rc = flock(lock_fd, LOCK_EX | LOCK_NB); - - if (rc || errno == EWOULDBLOCK) - { - syslog(LOG_ERR, "main: only one instance of %s is allowed", MPL); - exit(EXIT_FAILURE); - } - - syslog(LOG_NOTICE, "main: starting the %s daemon", MPL); - - mpl_create_dongle_dir(); - - GError *error = NULL; - GDBusConnection *connection = NULL; - - signal(SIGINT, signals_handler); - signal(SIGHUP, signals_handler); - signal(SIGTERM, signals_handler); - - connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (!connection) - { - syslog(LOG_ERR, "main, connection: couldn't get bus: %s", error->message); - exit(EXIT_FAILURE); - } - - cancellable = g_cancellable_new(); - loop = g_main_loop_new(NULL, FALSE); - - ctx = g_new0(Context, 1); - if (cancellable) - ctx->cancellable = g_object_ref(cancellable); - - mmcli_get_manager(connection, cancellable, (GAsyncReadyCallback)get_manager_ready, NULL); - g_main_loop_run(loop); - - if (cancellable) - g_object_unref(cancellable); - g_main_loop_unref(loop); - g_object_unref(connection); - - flock(lock_fd, LOCK_UN); - - syslog(LOG_NOTICE, "main: stopping the %s daemon", MPL); - closelog(); + mpl_stop(lock_fd); return EXIT_SUCCESS; } diff --git a/src/mportlink.h b/src/mportlink.h index a9e4f2c..cfcdfff 100644 --- a/src/mportlink.h +++ b/src/mportlink.h @@ -1,5 +1,14 @@ -#include "mm.h" +/* + * mportlink.h + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ -void mpl_create_dongle_dir(); -void mpl_device_added(MMManager *manager, MMObject *obj); -void mpl_device_removed(MMManager *manager, MMObject *obj); +#ifndef MPL_H_ +#define MPL_H_ + +#include "mportlink-lib.h" +#include "mportlink-core.h" + +#endif