1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* Copyright (C) 2004-2021 J.F.Dockes
* This 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 2 of the License, or
* (at your option) any later version.
*
* This 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, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "autoconfig.h"
#include <stdio.h>
#include "ecrontab.h"
#include "execmd.h"
#include "smallut.h"
#include "log.h"
using std::string;
using std::vector;
// Read crontab file and split it into lines.
static bool eCrontabGetLines(vector<string>& lines)
{
string crontab;
ExecCmd croncmd;
vector<string> args;
int status;
// Retrieve current crontab contents. An error here means that no
// crontab exists, and is not fatal, but we return a different
// status than for an empty one
args.push_back("-l");
if ((status = croncmd.doexec("crontab", args, nullptr, &crontab))) {
lines.clear();
return false;
}
// Split crontab into lines
stringToTokens(crontab, lines, "\n");
return true;
}
// Concatenate lines and write crontab
static bool eCrontabWriteFile(const vector<string>& lines, string& reason)
{
string crontab;
ExecCmd croncmd;
vector<string> args;
int status;
for (const auto& line : lines) {
crontab += line + "\n";
}
args.push_back("-");
if ((status = croncmd.doexec("crontab", args, &crontab, nullptr))) {
char nbuf[30];
snprintf(nbuf, sizeof(nbuf), "0x%x", status);
reason = string("Exec crontab -l failed: status: ") + nbuf;
return false;
}
return true;
}
// Add / change / delete entry identified by marker and id
bool editCrontab(const string& marker, const string& id,
const string& sched, const string& cmd, string& reason)
{
vector<string> lines;
if (!eCrontabGetLines(lines)) {
// Special case: cmd is empty, no crontab, don't create one
if (cmd.empty())
return true;
}
// Remove old copy if any
for (auto it = lines.begin(); it != lines.end(); it++) {
// Skip comment
if (it->find_first_of("#") == it->find_first_not_of(" \t"))
continue;
if (it->find(marker) != string::npos && it->find(id) != string::npos) {
lines.erase(it);
break;
}
}
if (!cmd.empty()) {
string nline = sched + " " + marker + " " + id + " " + cmd;
lines.push_back(nline);
}
if (!eCrontabWriteFile(lines, reason))
return false;
return true;
}
bool checkCrontabUnmanaged(const string& marker, const string& data)
{
vector<string> lines;
if (!eCrontabGetLines(lines)) {
// No crontab, answer is no
return false;
}
// Scan crontab
for (const auto& line : lines) {
if (line.find(marker) == string::npos &&
line.find(data) != string::npos) {
return true;
}
}
return false;
}
/** Retrieve the scheduling for a crontab entry */
bool getCrontabSched(const string& marker, const string& id,
vector<string>& sched)
{
LOGDEB0("getCrontabSched: marker[" << marker << "], id[" << id << "]\n");
vector<string> lines;
if (!eCrontabGetLines(lines)) {
// No crontab, answer is no
sched.clear();
return false;
}
string theline;
for (const auto& line : lines) {
// Skip comment
if (line.find_first_of("#") == line.find_first_not_of(" \t"))
continue;
if (line.find(marker) != string::npos &&
line.find(id) != string::npos) {
theline = line;
break;
}
}
stringToTokens(theline, sched);
sched.resize(5);
return true;
}
|