pax_global_header00006660000000000000000000000064131515343040014511gustar00rootroot0000000000000052 comment=f58d8e9794b9230811b6ac5567dceaa08d60e735 chkservice-0.1/000077500000000000000000000000001315153430400134775ustar00rootroot00000000000000chkservice-0.1/.gitignore000066400000000000000000000004241315153430400154670ustar00rootroot00000000000000build # Prerequisites *.d # Compiled Object files *.slo *.lo *.o *.obj # Precompiled Headers *.gch *.pch # Compiled Dynamic libraries *.so *.dylib *.dll # Fortran module files *.mod *.smod # Compiled Static libraries *.lai *.la *.a *.lib # Executables *.exe *.out *.app chkservice-0.1/.travis.yml000066400000000000000000000006051315153430400156110ustar00rootroot00000000000000sudo: true dist: trusty language: c++ env: - DEBUG=1 addons: apt: sources: - deadsnakes - ubuntu-toolchain-r-test install: - sudo apt-get update -qq - sudo do-release-upgrade -p -f DistUpgradeViewNonInteractive - sudo apt-get --yes install systemd cmake catch libsystemd-dev libncurses5-dev g++ ssh script: - mkdir build; cd build; cmake ../; make; sudo make Test chkservice-0.1/CMakeLists.txt000066400000000000000000000040301315153430400162340ustar00rootroot00000000000000project(chkservice) cmake_minimum_required(VERSION 2.8) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) if ($ENV{DEBUG}) message(STATUS "DEBUG mode enabled") set (CMAKE_CXX_FLAGS "-g -Wall") else() message(STATUS "DEBUG mode disabled") set (CMAKE_CXX_FLAGS "-O2") endif() if ($ENV{TRAVIS}) message(STATUS "Travis-CI build.") else() message(STATUS "Local build") endif() include_directories("${PROJECT_SOURCE_DIR}/include") find_package(PkgConfig REQUIRED) pkg_check_modules(SYSTEMD libsystemd REQUIRED) include_directories(${SYSTEMD_INCLUDE_DIRS}) set(LIBS ${LIBS} ${SYSTEMD_LIBRARIES}) pkg_check_modules(NCURSES ncurses REQUIRED) include_directories(${NCURSES_INCLUDE_DIRS}) set(LIBS ${LIBS} ${NCURSES_LIBRARIES}) add_subdirectory(${PROJECT_SOURCE_DIR}/src) if ($ENV{DEBUG}) message(STATUS "Build testing support") add_subdirectory(${PROJECT_SOURCE_DIR}/tests) endif() SET(MAJOR_VERSION 0) SET(MINOR_VERSION 1) SET(PATCH_VERSION 0) IF(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake") INCLUDE(InstallRequiredSystemLibraries) SET(CPACK_SET_DESTDIR "on") SET(CPACK_PACKAGING_INSTALL_PREFIX "/tmp") SET(CPACK_GENERATOR "DEB") SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Ncurses based gui for systemd") SET(CPACK_PACKAGE_VENDOR "Svetlana Linuxenko") SET(CPACK_PACKAGE_CONTACT "sveta@linuxenko.pro") SET(CPACK_PACKAGE_VERSION_MAJOR "${MAJOR_VERSION}") SET(CPACK_PACKAGE_VERSION_MINOR "${MINOR_VERSION}") SET(CPACK_PACKAGE_VERSION_PATCH "${PATCH_VERSION}") SET(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}_${MAJOR_VERSION}.${MINOR_VERSION}.${CPACK_PACKAGE_VERSION_PATCH}") SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}_${MAJOR_VERSION}.${MINOR_VERSION}.${CPACK_PACKAGE_VERSION_PATCH}") SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libncurses5 (>=5), libsystemd0 (>= 222)") SET(CPACK_DEBIAN_PACKAGE_PRIORITY "optional") SET(CPACK_DEBIAN_PACKAGE_SECTION "utils") SET(CPACK_DEBIAN_ARCHITECTURE ${CMAKE_SYSTEM_PROCESSOR}) SET(CPACK_COMPONENTS_ALL Libraries ApplicationData) INCLUDE(CPack) ENDIF(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake")chkservice-0.1/LICENSE000066400000000000000000000014411315153430400145040ustar00rootroot00000000000000chkservice is a tool for managing systemd units. more infomration at https://github.com/linuxenko/chkservice Copyright (C) 2017 Svetlana Linuxenko chkservice program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. chkservice 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, see . chkservice-0.1/README.md000066400000000000000000000033251315153430400147610ustar00rootroot00000000000000## chkservice [![Donate](https://img.shields.io/badge/donate-3$-green.svg?style=flat-square)](https://www.linuxenko.pro/donate.html#?amount=3) [![screenshot](https://raw.githubusercontent.com/linuxenko/linuxenko.github.io/master/media/chkservice/chkservice.png)](https://raw.githubusercontent.com/linuxenko/linuxenko.github.io/master/media/chkservice/chkservice.png) > chkservice is a tool for managing systemd units in terminal. ### Installation ``` sudo add-apt-repository ppa:linuxenko/chkservice sudo apt-get update sudo apt-get install chkservice ``` ### Usage `chkservice` require super user privileges to make changes into unit states or sysv scripts. For user it works read-only. ### Dependencies Package dependencies: * libncurses5 * libsystemd0 Build dependencies: * pkg-config * libncurses5-dev * libsystemd-dev ### Build Build and install debian package. ``` git clone https://github.com/linuxenko/chkservice.git mkdir build cd build cmake -DCMAKE_INSTALL_PREFIX=/usr ../ cpack dpkg -i chkservice-x.x.x.deb ``` Build release version. ``` git clone https://github.com/linuxenko/chkservice.git mkdir build cd build cmake ../ make ``` To build debug version, `DEBUG` environment should be set ``` export DEBUG=1 cmake .... make Test ``` [![Build Status](https://img.shields.io/travis/linuxenko/chkservice.svg?style=flat-square)](https://travis-ci.org/linuxenko/chkservice) until travis run it on `trusty` based containers ### TODO - [ ] - `.login1` target support (if someone request it) ### License GNU General Public License chkservice is a tool for managing systemd units. more infomration at https://github.com/linuxenko/chkservice Copyright (C) 2017 Svetlana Linuxenko chkservice-0.1/debian/000077500000000000000000000000001315153430400147215ustar00rootroot00000000000000chkservice-0.1/debian/changelog000066400000000000000000000007771315153430400166060ustar00rootroot00000000000000chkservice (0.1.0.2ppa0ubuntu2) xenial; urgency=medium * Updated launchpad build dependencies -- Svetlana Linuxenko Wed, 30 Aug 2017 14:38:21 +0300 chkservice (0.1.0.1ppa0ubuntu1) xenial; urgency=medium * Reupload first try -- Svetlana Linuxenko Wed, 30 Aug 2017 14:27:35 +0300 chkservice (0.1.0.0ppa0) sveta; urgency=medium * Initial release. (Closes: #XXXXXX) -- Svetlana Linuxenko Wed, 30 Aug 2017 13:36:50 +0300 chkservice-0.1/debian/control000066400000000000000000000005461315153430400163310ustar00rootroot00000000000000Source: chkservice Section: devel Priority: optional Maintainer: Svetlana Linuxenko Build-Depends: pkg-config, cmake, build-essential, libncurses5-dev, libsystemd-dev Homepage: https://github.com/linuxenko/chkservice Package: chkservice Architecture: any Depends: libncurses5, libsystemd0 Description: Ncurses based gui for systemd chkservice-0.1/debian/copyright000066400000000000000000000014411315153430400166540ustar00rootroot00000000000000chkservice is a tool for managing systemd units. more infomration at https://github.com/linuxenko/chkservice Copyright (C) 2017 Svetlana Linuxenko chkservice program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. chkservice 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, see . chkservice-0.1/debian/rules000077500000000000000000000010461315153430400160020ustar00rootroot00000000000000#!/usr/bin/make -f BUILDDIR = build_dir # secondly called by launchpad build: mkdir $(BUILDDIR); cd $(BUILDDIR); cmake -DCMAKE_INSTALL_PREFIX=../debian/tmp/usr .. make -C $(BUILDDIR) # thirdly called by launchpad binary: binary-indep binary-arch binary-indep: # nothing to be done binary-arch: cd $(BUILDDIR); cmake -P cmake_install.cmake mkdir debian/tmp/DEBIAN dpkg-gencontrol -pchkservice dpkg --build debian/tmp .. # firstly called by launchpad clean: rm -f build rm -rf $(BUILDDIR) .PHONY: binary binary-arch binary-indep clean chkservice-0.1/include/000077500000000000000000000000001315153430400151225ustar00rootroot00000000000000chkservice-0.1/include/chk-ctl.h000066400000000000000000000035201315153430400166200ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #ifndef _CHK_CTL_H #define _CHK_CTL_H #include "chk-systemd.h" typedef struct UnitItem { std::string id; std::string target; std::string description; int sub; int state; } UnitItem; enum { UNIT_STATE_DISABLED = 0x01, UNIT_STATE_ENABLED = 0x02, UNIT_STATE_STATIC = 0x03, UNIT_STATE_BAD = 0x04, UNIT_STATE_MASKED = 0x05, UNIT_STATE_TMP = 0x06 }; enum { UNIT_SUBSTATE_RUNNING = 0x1a, UNIT_SUBSTATE_CONNECTED = 0x2a, UNIT_SUBSTATE_INVALID = 0x3a, UNIT_SUBSTATE_TMP = 0x4a }; class ChkCTL { public: ChkCTL(); ~ChkCTL(); ChkBus *bus; std::vector getItemsSorted(); std::vector getByTarget(const char *target); std::vector getItems(); void toggleUnitState(UnitItem *item); void toggleUnitSubState(UnitItem *item); void fetch(); private: std::vector items; void pushItem(UnitInfo *unit); void sortByName(std::vector *sortable); }; #endif chkservice-0.1/include/chk-systemd.h000066400000000000000000000050331315153430400175270ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #ifndef _CHK_SYSTEMD_H #define _CHK_SYSTEMD_H #include #include #include #include #define ERR_PREFIX "Failed: " #define SYSV_INSTALL_EXEC "/lib/systemd/systemd-sysv-install" enum STATE_FLAGS { STATE_FLAGS_ENABLE, STATE_FLAGS_DISABLE, STATE_FLAGS_DISABLE_ISO }; typedef struct UnitInfo { const char *id; const char *description; const char *loadState; const char *activeState; const char *subState; const char *unitPath; const char *state; } UnitInfo; class ChkBus { public: ChkBus(); ~ChkBus(); bool connect(); void disconnect(); bool isConnected(); void setErrorMessage(int status); void setErrorMessage(const char *message); std::vector getUnits(); std::vector getUnitFiles(); std::vector getAllUnits(); void disableUnit(const char *name); void enableUnit(const char *name); void disableUnits(std::set *ids); void enableUnits(std::set *ids); void startUnit(const char *name); void stopUnit(const char *name); void startUnits(std::set *ids); void stopUnits(std::set *ids); static void freeUnitInfo(UnitInfo *unit); void reloadDaemon(); private: sd_bus* bus = NULL; std::string errorMessage; const char* getState(const char *name); void applyUnitState(const char *method, char **names, int flags); void applyUnitSub(const char *name, const char *method); void checkDisabledStatus(char **names); }; int busParseUnit(sd_bus_message *message, UnitInfo *u); void applySYSv(const char *state, const char **names); #endif chkservice-0.1/include/chk-ui.h000066400000000000000000000036451315153430400164630ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #ifndef _CHK_UI_H #define _CHKUI_H #include #include "chk-ctl.h" typedef struct RECTANGLE { int x; int y; int w; int h; } RECTANGLE; class MainWindow { public: static void sigwinch(int c); WINDOW *win; MainWindow(); ~MainWindow(); void createMenu(); private: RECTANGLE *screenSize = new RECTANGLE(); RECTANGLE *winSize = new RECTANGLE(); RECTANGLE *padding = new RECTANGLE(); ChkCTL *ctl = new ChkCTL; std::vector units; int selected = 0; int start = 0; void resize(); void moveUp(); void moveDown(); void movePageUp(); void movePageDown(); void drawUnits(); void drawItem(UnitItem *unit, int y); void drawInfo(); void toggleUnitState(); void toggleUnitSubState(); void updateUnits(); void error(char *err); void reloadAll(); }; void startCurses(); void stopCurses(); void printInMiddle(WINDOW *win, int starty, int startx, int width, char *string, chtype color, char *sp); void aboutWindow(RECTANGLE *parent); #endif chkservice-0.1/include/chk.h000066400000000000000000000027401315153430400160430ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #ifndef _CHK_H #define _CHK_H #define VERSION "0.1" #define ABOUT_INFO "\n\ chkservice %s \n\ \n\ Units list information:\n\ \n\ [x] - enabled unit. [ ] - disabled unit \n\ [s] - static unit. -m- - masked unit \n\ \n\ Navigation keys:\n\ \n\ Up/k - move cursor up. Down/j - move cursor down.\n\ PgUp/b - move page up. PgDown/f - move page down.\n\ \n\ Action keys:\n\ \n\ r - reload/update. q - exit.\n\ Space - enable/disable. s - start/stop unit.\n\ \n\ License:\n\ GPLv3 (c) Svetlana Linuxenko " #endif chkservice-0.1/src/000077500000000000000000000000001315153430400142665ustar00rootroot00000000000000chkservice-0.1/src/CMakeLists.txt000066400000000000000000000007051315153430400170300ustar00rootroot00000000000000add_library(CHKSYSTEMD chk-systemd.cpp chk-systemd-utils.cpp) target_link_libraries(CHKSYSTEMD ${LIBS}) add_library(CHKCTL chk-ctl.cpp) target_link_libraries(CHKCTL ${LIBS} CHKSYSTEMD) add_library(CHKUI chk-wmain.cpp chk-wutils.cpp) target_link_libraries(CHKUI ${LIBS} CHKSYSTEMD CHKCTL) add_executable(chkservice chkservice.cpp) target_link_libraries(chkservice ${LIBS} CHKSYSTEMD CHKCTL CHKUI) install(TARGETS chkservice RUNTIME DESTINATION sbin) chkservice-0.1/src/chk-ctl.cpp000066400000000000000000000125441315153430400163250ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #include #include "chk-ctl.h" #include "chk-systemd.h" ChkCTL::ChkCTL() { bus = new ChkBus(); items.clear(); } ChkCTL::~ChkCTL() { delete bus; items.clear(); } std::vector ChkCTL::getItems() { sortByName(&items); return items; } std::vector ChkCTL::getByTarget(const char *target) { std::vector found; std::string pattern = target == NULL ? "" : target; for (UnitItem *item : items) { if (item->target.find(pattern) == 0) { found.push_back(item); } } return found; } void ChkCTL::fetch() { std::vector sysUnits; for (auto item : items) { delete item; } items.clear(); items.shrink_to_fit(); try { sysUnits = bus->getAllUnits(); } catch (std::string &err) { throw err; } for (auto unit : sysUnits) { if (unit->id) { pushItem(unit); } delete unit; } sysUnits.clear(); sysUnits.shrink_to_fit(); } void ChkCTL::sortByName(std::vector *sortable) { std::sort(sortable->begin(), sortable->end(), [](const UnitItem *a, const UnitItem *b) { const char* s1 = a->id.c_str(); const char* s2 = b->id.c_str(); while(true) { if ( std::toupper(*s1) < std::toupper(*s2) ) return true; if ( std::toupper(*s1) > std::toupper(*s2) ) return false; if ( *s1 == 0 && *s2 == 0 ) return false; if ( *s1 > *s2) return false; if ( *s1 < *s2) return true; ++s1; ++s2; } }); } void ChkCTL::pushItem(UnitInfo *unit) { UnitItem *item = new UnitItem(); std::string id(unit->id); item->id = id; item->target = id.substr(id.find_last_of('.') + 1, id.length()); item->description = std::string((unit->description == NULL ? unit->unitPath : unit->description)); if (unit->state != NULL) { std::string state(unit->state); std::string sub(unit->subState == NULL ? "" : unit->subState); if (state.find("enabled") == 0) { item->state = UNIT_STATE_ENABLED; } else if (state.find("mask") == 0) { item->state = UNIT_STATE_MASKED; } else if (state.find("static") == 0) { item->state = UNIT_STATE_STATIC; } else if (state.find("bad") == 0 || state.find("removed") == 0) { item->state = UNIT_STATE_BAD; } else { item->state = UNIT_STATE_DISABLED; } if (!sub.empty()) { if (sub.find("running") == 0) { item->sub = UNIT_SUBSTATE_RUNNING; } else { item->sub = UNIT_SUBSTATE_CONNECTED; } } else { item->sub = UNIT_SUBSTATE_INVALID; } } else { item->state = UNIT_STATE_MASKED; } bus->freeUnitInfo(unit); items.push_back(item); }; std::vector ChkCTL::getItemsSorted() { std::vector orderedTargets; std::vector sunits; bool alreadyHasTarget = false; orderedTargets.push_back("service"); orderedTargets.push_back("timer"); orderedTargets.push_back("socket"); for (const auto unit : items) { alreadyHasTarget = false; for (auto t : orderedTargets) { if (t.find(unit->target) == 0) { alreadyHasTarget = true; break; } } if (!alreadyHasTarget) { orderedTargets.push_back(unit->target); } } bool isFirst = false; for (std::string target : orderedTargets) { auto targetedUnits = getByTarget(target.c_str()); sortByName(&targetedUnits); if (isFirst) { UnitItem *separatorTitle = new UnitItem(); UnitItem *separator = new UnitItem(); separatorTitle->target = target; sunits.push_back(separator); sunits.push_back(separatorTitle); sunits.push_back(separator); } sunits.insert(sunits.end(), targetedUnits.begin(), targetedUnits.end()); isFirst = true; } return sunits; } void ChkCTL::toggleUnitState(UnitItem *item) { try { if (item->state == UNIT_STATE_ENABLED || item->state == UNIT_STATE_STATIC) { if (item->sub == UNIT_SUBSTATE_RUNNING || item->sub == UNIT_SUBSTATE_CONNECTED) { bus->stopUnit(item->id.c_str()); } bus->disableUnit(item->id.c_str()); } else if (item->state == UNIT_STATE_DISABLED) { bus->enableUnit(item->id.c_str()); } item->state = UNIT_STATE_TMP; } catch (std::string &err) { throw err; } } void ChkCTL::toggleUnitSubState(UnitItem *item) { try { if (item->sub != UNIT_SUBSTATE_RUNNING) { bus->startUnit(item->id.c_str()); } else { bus->stopUnit(item->id.c_str()); } item->sub = UNIT_SUBSTATE_TMP; } catch (std::string &err) { throw err; } } chkservice-0.1/src/chk-systemd-utils.cpp000066400000000000000000000034161315153430400203670ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #include "chk-systemd.h" #include #include #include int busParseUnit(sd_bus_message *message, UnitInfo *u) { assert(message); assert(u); return sd_bus_message_read( message, "(ssssssouso)", &u->id, &u->description, &u->loadState, &u->activeState, &u->subState, NULL, &u->unitPath, NULL, NULL, NULL); } void applySYSv(const char *state, const char **names) { int pid = fork(); if (pid == 0) { for (int i = 0; names[i] != NULL; i++) { std::string unitName(names[i]); std::string sysvCMD = SYSV_INSTALL_EXEC; unitName = unitName.substr(0, unitName.find_last_of('.')); sysvCMD += " "; sysvCMD += state; sysvCMD += " "; sysvCMD += unitName; sysvCMD += " > /dev/null 2>&1"; system(sysvCMD.c_str()); } exit(0); } else { waitpid(pid, NULL, 0); } } chkservice-0.1/src/chk-systemd.cpp000066400000000000000000000277251315153430400172420ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #include #include #include #include "chk-systemd.h" ChkBus::ChkBus() { } ChkBus::~ChkBus() { disconnect(); } bool ChkBus::connect() { int status; errorMessage.clear(); if (isConnected()) { disconnect(); } status = sd_bus_open_system(&bus); if (status < 0) { setErrorMessage(status); disconnect(); throw std::string(errorMessage); } return isConnected(); } void ChkBus::disconnect() { if (bus != NULL) { sd_bus_unref(bus); bus = NULL; } } bool ChkBus::isConnected() { return bus == NULL ? false : true; } void ChkBus::setErrorMessage(int status) { errorMessage = (char *)ERR_PREFIX; if (status < 0) { status = -status; } errorMessage += strerror(status); } void ChkBus::setErrorMessage(const char *message) { errorMessage = (char *)ERR_PREFIX; errorMessage += message; } const char *ChkBus::getState(const char *name) { int status; const char *state; errorMessage.clear(); if (!isConnected()) { connect(); } sd_bus_message *busMessage = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; status = sd_bus_call_method( bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "GetUnitFileState", &error, &busMessage, "s", name); if (status < 0) { setErrorMessage(error.message); goto finish; } status = sd_bus_message_read(busMessage, "s", &state); if (status < 0) { setErrorMessage(status); goto finish; } finish: sd_bus_error_free(&error); sd_bus_message_unref(busMessage); if (state < 0) { throw std::string(errorMessage); } return status < 0 ? NULL : strdup(state); } std::vector ChkBus::getUnitFiles() { int status; const char *state; char *path; std::vector units; sd_bus_message* busMessage = NULL; sd_bus_message* reply = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; errorMessage.clear(); if (!isConnected()) { connect(); } status = sd_bus_message_new_method_call( bus, &busMessage, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "ListUnitFiles"); if (status < 0) { setErrorMessage(status); goto finish; } status = sd_bus_call(bus, busMessage, 0, &error, &reply); if (status < 0) { setErrorMessage(error.message); goto finish; } status = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)"); if (status < 0) { setErrorMessage(status); goto finish; } while ((status = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) { UnitInfo *unit = new UnitInfo(); unit->unitPath = strdup(path); unit->state = strdup(state); std::string p = std::string(unit->unitPath); unit->id = strdup(p.substr(p.find_last_of("/") + 1, p.length()).c_str()); units.push_back(unit); path = NULL; state = NULL; } status = sd_bus_message_exit_container(reply); if (status < 0) { setErrorMessage(status); goto finish; } finish: sd_bus_error_free(&error); sd_bus_message_unref(busMessage); sd_bus_message_unref(reply); disconnect(); if (status < 0) { throw std::string(errorMessage); } return units; } std::vector ChkBus::getUnits() { int status; UnitInfo unit; std::vector units; sd_bus_message* busMessage = NULL; sd_bus_message* reply = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; errorMessage.clear(); if (!isConnected()) { connect(); } status = sd_bus_message_new_method_call( bus, &busMessage, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "ListUnits" ); if (status < 0) { setErrorMessage(status); goto finish; } status = sd_bus_call(bus, busMessage, 0, &error, &reply); if (status < 0) { setErrorMessage(error.message); goto finish; } status = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)"); if (status < 0) { setErrorMessage(status); goto finish; } while ((status = busParseUnit(reply, &unit)) > 0) { UnitInfo *u = new UnitInfo(); u->id = strdup(unit.id); u->description = strdup(unit.description); u->loadState = strdup(unit.loadState); u->subState = strdup(unit.subState); u->unitPath = strdup(unit.unitPath); try { u->state = getState(unit.id); } catch (std::string &err) { break; } units.push_back(u); } status = sd_bus_message_exit_container(reply); if (status < 0) { setErrorMessage(status); goto finish; } finish: sd_bus_error_free(&error); sd_bus_message_unref(busMessage); sd_bus_message_unref(reply); disconnect(); if (status < 0) { throw std::string(errorMessage); } return units; } void ChkBus::freeUnitInfo(UnitInfo *unit) { free((void *)unit->id); free((void *)unit->unitPath); free((void *)unit->description); free((void *)unit->loadState); free((void *)unit->subState); } std::vector ChkBus::getAllUnits() { std::vector files; std::vector units; try { files = getUnitFiles(); units = getUnits(); } catch(std::string &err) { throw err; } for (auto unit : units) { int idx = 0; bool found = false; for (auto file : files) { std::string uid(unit->id); std::string fid(file->id); if (uid.find(fid) == 0) { freeUnitInfo(files[idx]); files[idx]->id = unit->id; files[idx]->unitPath = unit->unitPath; files[idx]->description = unit->description; files[idx]->loadState = unit->loadState; files[idx]->subState = unit->subState; found = true; break; } idx++; } if (!found) { files.push_back(unit); } else { delete unit; } } units.clear(); units.shrink_to_fit(); return files; } void ChkBus::reloadDaemon() { int status; sd_bus_message* busMessage = NULL; sd_bus_error error = SD_BUS_ERROR_NULL; if (!isConnected()) { connect(); } status = sd_bus_message_new_method_call( bus, &busMessage, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reload" ); if (status < 0) { setErrorMessage(status); goto finish; } sd_bus_call(bus, busMessage, 1000, &error, NULL); finish: sd_bus_error_free(&error); sd_bus_message_unref(busMessage); disconnect(); if (status < 0) { throw std::string(errorMessage); } } void ChkBus::applyUnitState(const char *method, char **names, int flags) { int status; bool checkState = false; sd_bus_error error = SD_BUS_ERROR_NULL; sd_bus_message *busMessage = NULL; if (!isConnected()) { connect(); } status = sd_bus_message_new_method_call( bus, &busMessage, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", method); if (status < 0) { setErrorMessage(status); goto finish; } status = sd_bus_message_append_strv(busMessage, names); if (status < 0) { setErrorMessage(status); goto finish; } switch(flags) { case STATE_FLAGS_ENABLE: status = sd_bus_message_append(busMessage, "bb", true, true); break; case STATE_FLAGS_DISABLE: status = sd_bus_message_append(busMessage, "b", true); checkState = true; break; case STATE_FLAGS_DISABLE_ISO: status = sd_bus_message_append(busMessage, "b", false); checkState = true; break; default: break; } if (status < 0) { setErrorMessage(status); goto finish; } status = sd_bus_call(bus, busMessage, 0, &error, NULL); if (status < 0) { setErrorMessage(error.message); goto finish; } if (flags == STATE_FLAGS_ENABLE) { applySYSv("enable", (const char **)names); } else if (flags == STATE_FLAGS_DISABLE) { applySYSv("disable", (const char **)names); } finish: sd_bus_error_free(&error); sd_bus_message_unref(busMessage); disconnect(); if (status < 0) { throw std::string(errorMessage); } if (checkState) { checkDisabledStatus(names); } } void ChkBus::checkDisabledStatus(char **names) { try { for (int i = 0; names[i] != NULL; i++) { const char *name = names[i]; const char *state = getState(name); if (std::string(state).find("enabled") == 0) { applyUnitState("DisableUnitFiles", names, STATE_FLAGS_DISABLE_ISO); applyUnitState("DisableUnitFiles", names, STATE_FLAGS_DISABLE); } if (state != NULL) { free((void *)state); } } } catch(std::string &err) { throw err; } } void ChkBus::applyUnitSub(const char *name, const char *method) { int status; sd_bus_error error = SD_BUS_ERROR_NULL; sd_bus_message *busMessage = NULL; sd_bus_message *reply = NULL; if (!isConnected()) { connect(); } status = sd_bus_call_method( bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", method, &error, &reply, "ss", name, "replace-irreversibly"); if (status < 0) { setErrorMessage(error.message); goto finish; } finish: sd_bus_error_free(&error); sd_bus_message_unref(busMessage); disconnect(); if (status < 0) { throw std::string(errorMessage); } } void ChkBus::enableUnits(std::set *ids) { int i = 0; if (ids->size() < 1) { return; } char *names[ids->size()]; for (auto id : (*ids)) { names[i] = (char *) id.c_str(); i++; } names[i] = NULL; try { applyUnitState("EnableUnitFiles", names, STATE_FLAGS_ENABLE); } catch (std::string &err) { throw err; } } void ChkBus::disableUnits(std::set *ids) { int i = 0; if (ids->size() < 1) { return; } char *names[ids->size()]; for (auto id : (*ids)) { names[i] = (char *) id.c_str(); i++; } names[i] = NULL; try { applyUnitState("DisableUnitFiles", names, STATE_FLAGS_DISABLE); } catch (std::string &err) { throw err; } } void ChkBus::enableUnit(const char *name) { try { std::set id; id.insert(name); enableUnits(&id); } catch (std::string &err) { throw err; } } void ChkBus::disableUnit(const char *name) { try { std::set id; id.insert(name); disableUnits(&id); } catch (std::string &err) { throw err; } } void ChkBus::stopUnits(std::set *ids) { try { for (auto id : (*ids)) { applyUnitSub(id.c_str(), "StopUnit"); } } catch (std::string &err) { throw err; } } void ChkBus::startUnits(std::set *ids) { try { for (auto id : (*ids)) { applyUnitSub(id.c_str(), "StartUnit"); } } catch (std::string &err) { throw err; } } void ChkBus::stopUnit(const char *name) { try { applyUnitSub(name, "StopUnit"); } catch (std::string &err) { throw err; } } void ChkBus::startUnit(const char *name) { try { applyUnitSub(name, "StartUnit"); } catch (std::string &err) { throw err; } } chkservice-0.1/src/chk-wmain.cpp000066400000000000000000000201041315153430400166450ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #include "chk.h" #include "chk-ui.h" #include "chk-systemd.h" #include #include #include #include #include MainWindow::MainWindow() { resize(); std::signal(SIGWINCH, MainWindow::sigwinch); padding->x = 2; padding->y = 2; } MainWindow::~MainWindow() { delwin(win); } void MainWindow::sigwinch(int c) { stopCurses(); exit(0); } void MainWindow::createMenu() { win = newwin(screenSize->h, screenSize->w, 0, 0); keypad(win, true); keypad(stdscr, true); while(1) { drawUnits(); int key = wgetch(win); error(NULL); switch(key) { case 'k': case KEY_UP: moveUp(); break; case 'j': case KEY_DOWN: moveDown(); break; case 'f': case KEY_NPAGE: movePageDown(); break; case 'b': case KEY_PPAGE: movePageUp(); break; case 'q': stopCurses(); delwin(win); exit(0); break; case ' ': toggleUnitState(); break; case 's': toggleUnitSubState(); break; case 'r': updateUnits(); drawUnits(); error((char *)"Updated.."); break; case '?': aboutWindow(screenSize); break; default: break; } } } void MainWindow::resize() { getmaxyx(stdscr, screenSize->h, screenSize->w); } void MainWindow::moveUp() { int ps = winSize->h - (padding->y + 1); if (start > 0 && selected < ps / 2) { start--; } else if (selected > 0) { selected--; } if (units[start + selected]->id.size() == 0) { moveUp(); } } void MainWindow::moveDown() { int offset = start + selected; int ps = winSize->h - (padding->y + 1); int max = units.size() - 1; if ((start + ps) < max) { if (selected < ps / 2) { selected++; } else { start++; } } else if (offset < max) { selected++; } if (offset >= max) { selected = ps; } if (units[start + selected]->id.size() == 0) { moveDown(); } } void MainWindow::movePageUp() { int ps = winSize->h - (padding->y + 1); if (start > 0) { start -= ps; } if (start < 0) { start = 0; selected = 0; } if (units[start + selected]->id.size() == 0) { moveUp(); } } void MainWindow::movePageDown() { int ps = winSize->h - 3; int max = units.size() - 1; if ((start + ps / 2) < max) { start += ps; } if ((start + ps) > max) { start = max - ps; selected = ps; } if (units[start + selected]->id.size() == 0) { moveDown(); } } void MainWindow::reloadAll() { try { ctl->bus->reloadDaemon(); updateUnits(); } catch (std::string &err) { error((char *)err.c_str()); } } void MainWindow::updateUnits() { units.clear(); units.shrink_to_fit(); try { ctl->fetch(); units = ctl->getItemsSorted(); } catch(std::string &err) { error((char *)err.c_str()); } } void MainWindow::drawUnits() { if (units.empty()) { updateUnits(); } getmaxyx(win, winSize->h, winSize->w); winSize->h -= padding->y; for (int i = 0; i < (winSize->h - padding->y); i++) { if ((i + start) > (int)units.size() - 1) { break; } UnitItem *unit = units[start + i]; if (i == selected) { wattron(win, A_REVERSE); } drawItem(unit, i + padding->y); wattroff(win, A_REVERSE); } drawInfo(); refresh(); wrefresh(win); } void MainWindow::drawItem(UnitItem *unit, int y) { if (unit->id.size() == 0) { if (unit->target.size() == 0) { printInMiddle(win, y, 0, winSize->w, (char *)"", COLOR_PAIR(3), (char *)' '); } else { std::string title(unit->target); title += "s"; title[0] = std::toupper(title[0]); printInMiddle(win, y, 0, winSize->w, (char *)"", COLOR_PAIR(3), (char *)' '); printInMiddle(win, y, 0, winSize->w / 2, (char *)title.c_str(), COLOR_PAIR(3), (char *)' '); } return; } // if (unit->id.find("acpid.service") == 0) { // std::cout << unit->id << " " << unit->state << UNIT_STATE_DISABLED << std::endl; // exit(0); // } if (unit->state == UNIT_STATE_ENABLED) { wattron(win, COLOR_PAIR(2)); mvwprintw(win, y, padding->x, "[x]"); wattroff(win, COLOR_PAIR(2)); } else if (unit->state == UNIT_STATE_DISABLED) { wattron(win, COLOR_PAIR(5)); mvwprintw(win, y, padding->x, "[ ]"); wattroff(win, COLOR_PAIR(5)); } else if (unit->state == UNIT_STATE_STATIC) { wattron(win, COLOR_PAIR(5)); mvwprintw(win, y, padding->x, "[s]"); wattroff(win, COLOR_PAIR(5)); } else if (unit->state == UNIT_STATE_BAD) { wattron(win, COLOR_PAIR(1)); mvwprintw(win, y, padding->x, "-b-"); wattroff(win, COLOR_PAIR(1)); } else if (unit->state == UNIT_STATE_MASKED) { wattron(win, COLOR_PAIR(3)); mvwprintw(win, y, padding->x, "-m-"); wattroff(win, COLOR_PAIR(3)); } if (unit->sub == UNIT_SUBSTATE_RUNNING) { wattron(win, COLOR_PAIR(3)); mvwprintw(win, y, padding->x + 3, " > "); wattroff(win, COLOR_PAIR(3)); } else if (unit->sub == UNIT_SUBSTATE_CONNECTED) { wattron(win, COLOR_PAIR(5)); mvwprintw(win, y, padding->x + 3, " = "); wattroff(win, COLOR_PAIR(5)); } else { wattron(win, COLOR_PAIR(5)); mvwprintw(win, y, padding->x + 3, " "); wattroff(win, COLOR_PAIR(5)); } unsigned int leftPad = padding->x + 8; unsigned int rightPad = (winSize->w - leftPad); if (unit->id.size() > (rightPad - padding->x)) { unit->id.resize(rightPad - padding->x); } std::stringstream sline; unit->description.resize(winSize->w / 2, ' '); sline << std::string(unit->id.size(), ' ') << " " << std::setw(rightPad - unit->id.size()) << unit->description; std::string cline(sline.str()); std::string name(unit->id); name.resize(cline.find_first_of(unit->description[0]), ' '); if (cline.size() > rightPad) { cline.resize(rightPad - 2 ); } wattron(win, COLOR_PAIR(4)); mvwprintw(win, y, leftPad, "%s", cline.c_str()); wattroff(win, COLOR_PAIR(4)); mvwprintw(win, y, leftPad, "%s", name.c_str()); } void MainWindow::drawInfo() { int count = 0; int countUntilNow = start + selected; for (auto unit : units) { if (unit->id.size() == 0) { if (countUntilNow > count) { countUntilNow--; } continue; } count++; } std::stringstream position; position << countUntilNow + 1 << "/" << count; printInMiddle(win, winSize->h + 1, 0, winSize->w, (char *)"", COLOR_PAIR(5), (char *)' '); printInMiddle(win, winSize->h + 1, 0, (winSize->w / 2), (char *)position.str().c_str(), COLOR_PAIR(5), (char *)NULL); wattron(win, COLOR_PAIR(4)); mvwprintw(win, winSize->h + 1, winSize->w - 10, "? - help"); wattroff(win, COLOR_PAIR(4)); } void MainWindow::error(char *err) { mvwprintw(win, 0, 0, std::string(winSize->w, ' ').c_str()); if (err) { mvwprintw(win, 0, 1, err); } } void MainWindow::toggleUnitState() { try { ctl->toggleUnitState(units[start + selected]); reloadAll(); } catch (std::string &err) { error((char *)err.c_str()); } } void MainWindow::toggleUnitSubState() { try { ctl->toggleUnitSubState(units[start + selected]); reloadAll(); } catch (std::string &err) { error((char *)err.c_str()); } } chkservice-0.1/src/chk-wutils.cpp000066400000000000000000000043441315153430400170710ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #include "chk-ui.h" #include "chk.h" void startCurses() { initscr(); start_color(); noecho(); cbreak(); curs_set(false); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_BLUE, COLOR_BLACK); init_pair(4, COLOR_CYAN, COLOR_BLACK); init_pair(5, COLOR_MAGENTA, COLOR_BLACK); } void stopCurses() { endwin(); } void printInMiddle(WINDOW *win, int starty, int startx, int width, char *string, chtype color, char *sp) { int length, x, y; float temp; if(win == NULL) win = stdscr; getyx(win, y, x); if(startx != 0) x = startx; if(starty != 0) y = starty; if(width == 0) width = 80; length = strlen(string); temp = (width - length)/ 2; x = startx + (int)temp; wattron(win, color); if (sp) { for (int i = startx; i < width; i++) { mvwprintw(win, y, i, "%c", sp); } } mvwprintw(win, y, x, "%s", string); wattroff(win, color); } void aboutWindow(RECTANGLE *parent) { const int winH = 22; const int winW = 60; WINDOW *aboutwin = newwin(winH, winW, (parent->h / 2) - (winH / 2), (parent->w / 2) - (winW / 2) ); wattron(aboutwin, COLOR_PAIR(2)); mvwprintw(aboutwin, 0, 0, ABOUT_INFO, VERSION); wattroff(aboutwin, COLOR_PAIR(2)); refresh(); wrefresh(aboutwin); getch(); delwin(aboutwin); clear(); refresh(); } chkservice-0.1/src/chkservice.cpp000066400000000000000000000023711315153430400171230ustar00rootroot00000000000000/* * chkservice is a tool for managing systemd units. * more infomration at https://github.com/linuxenko/chkservice * * Copyright (C) 2017 Svetlana Linuxenko * * chkservice program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * chkservice 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, see . */ #include #include "chk-systemd.h" #include "chk-ctl.h" #include "chk-ui.h" #include "chk.h" using namespace std; int main(int ac, char **av) { if (ac > 1) { fprintf(stdout, ABOUT_INFO, VERSION); return 0; } startCurses(); MainWindow *mainWindow = new MainWindow(); mainWindow->createMenu(); delete mainWindow; stopCurses(); return 0; } chkservice-0.1/tests/000077500000000000000000000000001315153430400146415ustar00rootroot00000000000000chkservice-0.1/tests/CMakeLists.txt000066400000000000000000000003131315153430400173760ustar00rootroot00000000000000add_executable(RunTests main-test.cpp chksystemd-test.cpp chkctl-test.cpp chkui-test.cpp) target_link_libraries(RunTests ${LIBS} CHKSYSTEMD CHKCTL CHKUI) add_custom_target(Test COMMAND sudo ./RunTests) chkservice-0.1/tests/chkctl-test.cpp000066400000000000000000000033471315153430400176010ustar00rootroot00000000000000#include #include #include "chk-ctl.h" using namespace std; TEST_CASE("should create object ctl", "[ChkCTL]") { ChkCTL *ctl = new ChkCTL(); REQUIRE(ctl != NULL); delete ctl; } TEST_CASE("should return sorted items", "[ChkCTL]") { ChkCTL *ctl = new ChkCTL(); REQUIRE_NOTHROW(ctl->fetch()); auto units = ctl->getItems(); REQUIRE(tolower(units[0]->id[0]) < tolower(units[units.size() - 1]->id[0])); units = ctl->getByTarget("service"); REQUIRE(tolower(units[0]->id[0]) < tolower(units[units.size() - 1]->id[0])); delete ctl; } TEST_CASE("should get system units against saved items ", "[ChkCTL]") { ChkCTL *ctl = new ChkCTL(); ctl->fetch(); auto items = ctl->getItemsSorted(); REQUIRE(items.size() > 0); delete ctl; } TEST_CASE("should fetch units and prepare", "[ChkCTL]") { ChkCTL *ctl = new ChkCTL(); ctl->fetch(); bool filtered = true; for (auto unit : ctl->getByTarget("service")) { if (unit->target.find("service") != 0) { filtered = false; } } for (auto unit : ctl->getByTarget("device")) { if (unit->target.find("device") != 0) { filtered = false; } } REQUIRE(filtered == true); delete ctl; } TEST_CASE("should fetch items sorted by target", "[ChkCTL]") { ChkCTL *ctl = new ChkCTL(); ctl->fetch(); vector units = ctl->getItemsSorted(); string lastTarget; int targetChanged = 0; std::set targets; for (const auto unit : units) { targets.insert((*unit).target); } for (auto unit : units) { if (unit->id.size() != 0 && unit->target.compare(lastTarget) != 0) { lastTarget = unit->target; targetChanged++; } } REQUIRE(targetChanged == (targets.size() - 1)); delete ctl; } chkservice-0.1/tests/chksystemd-test.cpp000066400000000000000000000054301315153430400205020ustar00rootroot00000000000000#include #include #include #include "chk-systemd.h" using namespace std; TEST_CASE("should create object bus", "[ChkBus]") { ChkBus *bus = new ChkBus(); REQUIRE(bus != NULL); REQUIRE(bus->isConnected() == false); delete bus; } TEST_CASE("should connect to systemd bus", "[ChkBus]") { ChkBus *bus = new ChkBus(); REQUIRE_NOTHROW(bus->connect()); REQUIRE(bus->isConnected() == true); bus->disconnect(); REQUIRE(bus->isConnected() == false); delete bus; } TEST_CASE("should get list of unit files", "[ChkBus]") { ChkBus *bus = new ChkBus(); bool sshServiceFound = false; vector files = bus->getUnitFiles(); int foundCount = 0; for (auto file : files) { string id(file->id); string path(file->unitPath); if (path.find(id) == std::string::npos) { REQUIRE( true == false ); } } delete bus; } TEST_CASE("should get list of units", "[ChkBus]") { ChkBus *bus = new ChkBus(); bool sshServiceFound = false; REQUIRE_NOTHROW(bus->connect()); vector units; REQUIRE_NOTHROW((units = bus->getAllUnits())); REQUIRE(typeid(*units[0]) == typeid(UnitInfo)); REQUIRE(typeid(units[0]->id) == typeid(const char *)); REQUIRE(string(units[0]->id).length() > 0); for (auto unit : units) { if (string(unit->id).find("ssh.service") == 0) { sshServiceFound = true; } } REQUIRE(sshServiceFound == true); delete bus; } TEST_CASE("should get all units", "[ChkBus]") { ChkBus *bus = new ChkBus(); auto allUnits = bus->getAllUnits(); auto units = bus->getUnits(); REQUIRE(allUnits.size() > units.size()); allUnits.clear(); units.clear(); delete bus; } TEST_CASE("should be able enable/disable unit", "[ChkBus]") { ChkBus *bus = new ChkBus(); REQUIRE_NOTHROW(bus->enableUnit("ssh.service")); for (auto unit : bus->getAllUnits()) { if (string(unit->id).find("ssh.service") == 0) { REQUIRE(string(unit->state).find("enabled") == 0); break; } } REQUIRE_NOTHROW(bus->disableUnit("ssh.service")); REQUIRE_NOTHROW(bus->reloadDaemon()); for (auto unit : bus->getAllUnits()) { if (string(unit->id).find("ssh.service") == 0) { REQUIRE(string(unit->state).find("disabled") == 0); break; } } delete bus; } TEST_CASE("should be able start/stop unit", "[ChkBus]") { ChkBus *bus = new ChkBus(); REQUIRE_NOTHROW(bus->stopUnit("ssh.service")); for (auto unit : bus->getAllUnits()) { if (string(unit->id).compare("ssh.service") == 0) { REQUIRE(unit->loadState == NULL); break; } } REQUIRE_NOTHROW(bus->startUnit("ssh.service")); for (auto unit : bus->getAllUnits()) { if (string(unit->id).compare("ssh.service") == 0) { REQUIRE(unit->loadState != NULL); break; } } delete bus; } chkservice-0.1/tests/chkui-test.cpp000066400000000000000000000003411315153430400174230ustar00rootroot00000000000000#include #include #include "chk-ui.h" using namespace std; TEST_CASE("should create window", "[ChkUI]") { MainWindow *win = new MainWindow(); // REQUIRE(win->screenSize->w > 0); delete win; } chkservice-0.1/tests/main-test.cpp000066400000000000000000000002211315153430400172410ustar00rootroot00000000000000#define CATCH_CONFIG_MAIN #include #include TEST_CASE( "should initialize tests", "[Catch]" ) { REQUIRE( 1 == 1 ); }