chk-systemd-utils.cpp 1.76 KB
Newer Older
Ana Custura's avatar
Ana Custura committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/*
 *    chkservice is a tool for managing systemd units.
 *    more infomration at https://github.com/linuxenko/chkservice
 *
 *    Copyright (C) 2017 Svetlana Linuxenko <svetlana(at)linuxenko.pro>
 *
 *    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 <http://www.gnu.org/licenses/>.
 */

#include "chk-systemd.h"
#include <cassert>
#include <unistd.h>
#include <sys/wait.h>

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);
  }
}