| 1 | /**************************************************************************
|
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> *
|
|---|
| 3 | * *
|
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp *
|
|---|
| 5 | * *
|
|---|
| 6 | * This program is free software; you can redistribute it and/or modify *
|
|---|
| 7 | * it under the terms of the GNU General Public License as published by *
|
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or *
|
|---|
| 9 | * (at your option) any later version. *
|
|---|
| 10 | * *
|
|---|
| 11 | * This program is distributed in the hope that it will be useful, *
|
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|---|
| 14 | * GNU General Public License for more details. *
|
|---|
| 15 | * *
|
|---|
| 16 | * You should have received a copy of the GNU General Public License *
|
|---|
| 17 | * along with this program; if not, write to the *
|
|---|
| 18 | * Free Software Foundation, Inc., *
|
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|---|
| 20 | ***************************************************************************/
|
|---|
| 21 | #pragma once
|
|---|
| 22 |
|
|---|
| 23 | #include <string>
|
|---|
| 24 | #include <map>
|
|---|
| 25 | #include <strEx.h>
|
|---|
| 26 | #include <error.hpp>
|
|---|
| 27 | #include <filter_framework.hpp>
|
|---|
| 28 | #include <error_com.hpp>
|
|---|
| 29 | #include <Mstask.h>
|
|---|
| 30 |
|
|---|
| 31 | class TaskSched
|
|---|
| 32 | {
|
|---|
| 33 | public:
|
|---|
| 34 | struct result {
|
|---|
| 35 | struct fetch_key {
|
|---|
| 36 | bool nextRunTime;
|
|---|
| 37 | bool triggers;
|
|---|
| 38 | bool idleMinutes;
|
|---|
| 39 | bool deadlineMinutes;
|
|---|
| 40 | bool mostRecentRunTime;
|
|---|
| 41 | bool status;
|
|---|
| 42 | bool exitCode;
|
|---|
| 43 | bool comment;
|
|---|
| 44 | bool creator;
|
|---|
| 45 | bool data;
|
|---|
| 46 | bool flags;
|
|---|
| 47 | bool accountName;
|
|---|
| 48 | bool workingDirectory;
|
|---|
| 49 | bool priority;
|
|---|
| 50 | bool maxRunTime;
|
|---|
| 51 | bool applicationName;
|
|---|
| 52 | bool parameters;
|
|---|
| 53 | fetch_key() :
|
|---|
| 54 | nextRunTime(false),
|
|---|
| 55 | triggers(false),
|
|---|
| 56 | idleMinutes(false),
|
|---|
| 57 | deadlineMinutes(false),
|
|---|
| 58 | mostRecentRunTime(false),
|
|---|
| 59 | status(false),
|
|---|
| 60 | exitCode(false),
|
|---|
| 61 | comment(false),
|
|---|
| 62 | creator(false),
|
|---|
| 63 | data(false),
|
|---|
| 64 | flags(false),
|
|---|
| 65 | accountName(false),
|
|---|
| 66 | workingDirectory(false),
|
|---|
| 67 | priority(false),
|
|---|
| 68 | maxRunTime(false),
|
|---|
| 69 | applicationName(false),
|
|---|
| 70 | parameters(false)
|
|---|
| 71 | {}
|
|---|
| 72 | fetch_key(bool bval) :
|
|---|
| 73 | nextRunTime(bval),
|
|---|
| 74 | triggers(bval),
|
|---|
| 75 | idleMinutes(bval),
|
|---|
| 76 | deadlineMinutes(bval),
|
|---|
| 77 | mostRecentRunTime(bval),
|
|---|
| 78 | status(bval),
|
|---|
| 79 | exitCode(bval),
|
|---|
| 80 | comment(bval),
|
|---|
| 81 | creator(bval),
|
|---|
| 82 | data(bval),
|
|---|
| 83 | flags(bval),
|
|---|
| 84 | accountName(bval),
|
|---|
| 85 | workingDirectory(bval),
|
|---|
| 86 | priority(bval),
|
|---|
| 87 | maxRunTime(bval),
|
|---|
| 88 | applicationName(bval),
|
|---|
| 89 | parameters(bval)
|
|---|
| 90 | {}
|
|---|
| 91 | };
|
|---|
| 92 | struct task_sched_date {
|
|---|
| 93 | unsigned long long date_;
|
|---|
| 94 | bool never_;
|
|---|
| 95 | bool has_value_;
|
|---|
| 96 | task_sched_date() : never_(false), has_value_(false) {}
|
|---|
| 97 | void operator=(unsigned long long date) {
|
|---|
| 98 | has_value_ = true;
|
|---|
| 99 | date_ = date;
|
|---|
| 100 | never_ = false;
|
|---|
| 101 | }
|
|---|
| 102 | operator unsigned long long () {
|
|---|
| 103 | return date_;
|
|---|
| 104 | }
|
|---|
| 105 | operator const unsigned long long () const {
|
|---|
| 106 | return date_;
|
|---|
| 107 | }
|
|---|
| 108 | /*
|
|---|
| 109 | operator unsigned __int64 () {
|
|---|
| 110 | return date_;
|
|---|
| 111 | }
|
|---|
| 112 | */
|
|---|
| 113 | };
|
|---|
| 114 | task_sched_date nextRunTime;
|
|---|
| 115 | std::list<std::wstring> triggers;
|
|---|
| 116 | WORD idleMinutes;
|
|---|
| 117 | WORD deadlineMinutes;
|
|---|
| 118 | task_sched_date mostRecentRunTime;
|
|---|
| 119 | HRESULT status;
|
|---|
| 120 | DWORD exitCode;
|
|---|
| 121 | std::wstring comment;
|
|---|
| 122 | std::wstring creator;
|
|---|
| 123 | std::wstring data;
|
|---|
| 124 | std::wstring parameters;
|
|---|
| 125 | DWORD flags;
|
|---|
| 126 | std::wstring accountName;
|
|---|
| 127 | std::wstring workingDirectory;
|
|---|
| 128 | DWORD priority;
|
|---|
| 129 | DWORD maxRunTime;
|
|---|
| 130 | std::wstring title;
|
|---|
| 131 | std::wstring applicationName;
|
|---|
| 132 |
|
|---|
| 133 | unsigned long long ullNow;
|
|---|
| 134 |
|
|---|
| 135 | #define accountName_alias "account-name"
|
|---|
| 136 | #define applicationName_alias "application-name"
|
|---|
| 137 | #define workingDirectory_alias "working-directory"
|
|---|
| 138 | #define exitCode_alias "exit-code"
|
|---|
| 139 | #define nextRunTime_alias "next-run-time"
|
|---|
| 140 | #define mostRecentRunTime_alias "most-recent-run-time"
|
|---|
| 141 |
|
|---|
| 142 | #define REPLACER_NORMAL(key) \
|
|---|
| 143 | strEx::replace(format, _T("%") _T(#key) _T("%"), key);
|
|---|
| 144 | #define REPLACER_ALIAS(key) \
|
|---|
| 145 | strEx::replace(format, _T("%") _T(key ## _alias) _T("%"), key);
|
|---|
| 146 | #define REPLACER_R_NORMAL(key) \
|
|---|
| 147 | strEx::replace(format, _T("%") _T(#key) _T("%"), render_ ## key());
|
|---|
| 148 | #define REPLACER_R_ALIAS(key) \
|
|---|
| 149 | strEx::replace(format, _T("%") _T(key ## _alias) _T("%"), render_ ## key());
|
|---|
| 150 | #define REPLACER_I_NORMAL(key) \
|
|---|
| 151 | strEx::replace(format, _T("%") _T(#key) _T("%"), strEx::itos(key));
|
|---|
| 152 | #define REPLACER_I_ALIAS(key) \
|
|---|
| 153 | strEx::replace(format, _T("%") _T(key ## _alias) _T("%"), strEx::itos(key));
|
|---|
| 154 | #define REPLACER_D_EX(key, val) \
|
|---|
| 155 | if (key.never_ == true) \
|
|---|
| 156 | strEx::replace(format, _T("%") _T(key ## _alias) _T("%"), _T("never")); \
|
|---|
| 157 | else if (key.has_value_) { \
|
|---|
| 158 | strEx::replace(format, _T("%") _T(val) _T("%"), strEx::format_filetime(key)); \
|
|---|
| 159 | }
|
|---|
| 160 | #define REPLACER_D_NORMAL(key) REPLACER_D_EX(key, #key)
|
|---|
| 161 | #define REPLACER_D_ALIAS(key) REPLACER_D_EX(key, key ## _alias)
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | std::wstring render(std::wstring format) const {
|
|---|
| 165 | REPLACER_NORMAL(title);
|
|---|
| 166 | REPLACER_ALIAS(accountName);
|
|---|
| 167 | REPLACER_ALIAS(applicationName);
|
|---|
| 168 | REPLACER_NORMAL(comment);
|
|---|
| 169 | REPLACER_NORMAL(creator);
|
|---|
| 170 | REPLACER_NORMAL(data);
|
|---|
| 171 | REPLACER_ALIAS(workingDirectory);
|
|---|
| 172 | REPLACER_I_ALIAS(exitCode);
|
|---|
| 173 | REPLACER_R_NORMAL(flags);
|
|---|
| 174 | REPLACER_NORMAL(parameters);
|
|---|
| 175 | //REPLACER_D_ALIAS(nextRunTime);
|
|---|
| 176 | REPLACER_D_ALIAS(mostRecentRunTime);
|
|---|
| 177 |
|
|---|
| 178 | strEx::replace(format, _T("\n"), _T(""));
|
|---|
| 179 | return format;
|
|---|
| 180 | }
|
|---|
| 181 | std::wstring render_exitCode() const {
|
|---|
| 182 | return strEx::itos(exitCode);
|
|---|
| 183 | }
|
|---|
| 184 | #define RENDER_FLAG(key) if ((flags&key)!=0) strEx::append_list(ret, std::wstring(_T(#key)))
|
|---|
| 185 |
|
|---|
| 186 | std::wstring render_flags() const {
|
|---|
| 187 | std::wstring ret;
|
|---|
| 188 | RENDER_FLAG(TASK_FLAG_INTERACTIVE);
|
|---|
| 189 | RENDER_FLAG(TASK_FLAG_DELETE_WHEN_DONE);
|
|---|
| 190 | RENDER_FLAG(TASK_FLAG_DISABLED);
|
|---|
| 191 | RENDER_FLAG(TASK_FLAG_HIDDEN);
|
|---|
| 192 | RENDER_FLAG(TASK_FLAG_RUN_ONLY_IF_LOGGED_ON);
|
|---|
| 193 | RENDER_FLAG(TASK_FLAG_START_ONLY_IF_IDLE);
|
|---|
| 194 | RENDER_FLAG(TASK_FLAG_SYSTEM_REQUIRED);
|
|---|
| 195 | RENDER_FLAG(TASK_FLAG_KILL_ON_IDLE_END);
|
|---|
| 196 | RENDER_FLAG(TASK_FLAG_RESTART_ON_IDLE_RESUME);
|
|---|
| 197 | RENDER_FLAG(TASK_FLAG_DONT_START_IF_ON_BATTERIES);
|
|---|
| 198 | RENDER_FLAG(TASK_FLAG_KILL_IF_GOING_ON_BATTERIES);
|
|---|
| 199 | RENDER_FLAG(TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET);
|
|---|
| 200 | RENDER_FLAG(TASK_FLAG_INTERACTIVE);
|
|---|
| 201 | return ret;
|
|---|
| 202 | }
|
|---|
| 203 | };
|
|---|
| 204 | typedef std::list<result> result_type;
|
|---|
| 205 | struct wmi_filter {
|
|---|
| 206 | #define STRING_FILTER(key) filters::filter_all_strings key;
|
|---|
| 207 | #define NUMERIC_FILTER(key) filters::filter_all_numeric<unsigned long, checkHolders::int_handler > key;
|
|---|
| 208 | static const __int64 MSECS_TO_100NS = 10000;
|
|---|
| 209 |
|
|---|
| 210 | STRING_FILTER(title);
|
|---|
| 211 | STRING_FILTER(accountName);
|
|---|
| 212 | STRING_FILTER(applicationName);
|
|---|
| 213 | STRING_FILTER(comment);
|
|---|
| 214 | STRING_FILTER(creator);
|
|---|
| 215 | STRING_FILTER(data);
|
|---|
| 216 | STRING_FILTER(workingDirectory);
|
|---|
| 217 | NUMERIC_FILTER(exitCode);
|
|---|
| 218 | NUMERIC_FILTER(flags);
|
|---|
| 219 | STRING_FILTER(parameters);
|
|---|
| 220 | filters::filter_all_times nextRunTime;
|
|---|
| 221 | filters::filter_all_times mostRecentRunTime;
|
|---|
| 222 |
|
|---|
| 223 | #define HAS_FILTER(key) || key.hasFilter()
|
|---|
| 224 |
|
|---|
| 225 | inline bool hasFilter() {
|
|---|
| 226 | return false HAS_FILTER(title)
|
|---|
| 227 | HAS_FILTER(accountName)
|
|---|
| 228 | HAS_FILTER(applicationName)
|
|---|
| 229 | HAS_FILTER(comment)
|
|---|
| 230 | HAS_FILTER(creator)
|
|---|
| 231 | HAS_FILTER(data)
|
|---|
| 232 | HAS_FILTER(workingDirectory)
|
|---|
| 233 | HAS_FILTER(exitCode)
|
|---|
| 234 | HAS_FILTER(flags)
|
|---|
| 235 | HAS_FILTER(nextRunTime)
|
|---|
| 236 | HAS_FILTER(mostRecentRunTime)
|
|---|
| 237 | HAS_FILTER(parameters);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | #define MATCH_FILTER(key) if ((key.hasFilter())&&(key.matchFilter(value.key))) return true;
|
|---|
| 241 | bool matchFilter(const result &value) const {
|
|---|
| 242 | MATCH_FILTER(title);
|
|---|
| 243 | MATCH_FILTER(accountName);
|
|---|
| 244 | MATCH_FILTER(applicationName);
|
|---|
| 245 | MATCH_FILTER(comment);
|
|---|
| 246 | MATCH_FILTER(creator);
|
|---|
| 247 | MATCH_FILTER(data);
|
|---|
| 248 | MATCH_FILTER(parameters);
|
|---|
| 249 | MATCH_FILTER(workingDirectory);
|
|---|
| 250 | MATCH_FILTER(exitCode);
|
|---|
| 251 | MATCH_FILTER(flags);
|
|---|
| 252 |
|
|---|
| 253 | if ((nextRunTime.hasFilter())&&(nextRunTime.matchFilter((value.ullNow-value.nextRunTime)/MSECS_TO_100NS)))
|
|---|
| 254 | return true;
|
|---|
| 255 | if ((mostRecentRunTime.hasFilter())&&(mostRecentRunTime.matchFilter((value.ullNow-value.mostRecentRunTime)/MSECS_TO_100NS)))
|
|---|
| 256 | return true;
|
|---|
| 257 |
|
|---|
| 258 | MATCH_FILTER(parameters);
|
|---|
| 259 | return false;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | #define DEBUG_FILTER(key) if (key.hasFilter()) ss << _T(" ") << _T( # key) << _T(" = ") << key.to_string() << std::endl;
|
|---|
| 263 |
|
|---|
| 264 | std::wstring to_string() const {
|
|---|
| 265 | std::wstringstream ss;
|
|---|
| 266 | ss << _T("filter = {") << std::endl;
|
|---|
| 267 | DEBUG_FILTER(title);
|
|---|
| 268 | DEBUG_FILTER(accountName);
|
|---|
| 269 | DEBUG_FILTER(applicationName);
|
|---|
| 270 | DEBUG_FILTER(comment);
|
|---|
| 271 | DEBUG_FILTER(creator);
|
|---|
| 272 | DEBUG_FILTER(data);
|
|---|
| 273 | DEBUG_FILTER(parameters);
|
|---|
| 274 | DEBUG_FILTER(workingDirectory);
|
|---|
| 275 | DEBUG_FILTER(exitCode);
|
|---|
| 276 | DEBUG_FILTER(flags);
|
|---|
| 277 | DEBUG_FILTER(nextRunTime);
|
|---|
| 278 | DEBUG_FILTER(mostRecentRunTime);
|
|---|
| 279 | DEBUG_FILTER(parameters);
|
|---|
| 280 | ss << _T(" }") << std::endl;
|
|---|
| 281 | return ss.str();
|
|---|
| 282 | }
|
|---|
| 283 | };
|
|---|
| 284 | class Exception {
|
|---|
| 285 | std::wstring message_;
|
|---|
| 286 | public:
|
|---|
| 287 | Exception(std::wstring str, HRESULT code) {
|
|---|
| 288 | message_ = str + _T(":") + error::format::from_system(code);
|
|---|
| 289 | }
|
|---|
| 290 | std::wstring getMessage() {
|
|---|
| 291 | return message_;
|
|---|
| 292 | }
|
|---|
| 293 | };
|
|---|
| 294 |
|
|---|
| 295 | result_type findAll(result::fetch_key key);
|
|---|
| 296 | std::wstring sanitize_string(LPTSTR in);
|
|---|
| 297 |
|
|---|
| 298 | };
|
|---|