Warning, file /pim/kalarm/src/rtcwakeaction.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  rtcwakeaction.cpp  -  KAuth helper application to execute rtcwake commands
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2011-2019 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "rtcwakeaction.h"
0010 
0011 #include "kalarm_debug.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QProcess>
0016 #include <QDateTime>
0017 
0018 #include <stdio.h>
0019 #ifdef Q_OS_WIN
0020 #define popen _popen
0021 #define pclose _pclose
0022 #endif
0023 
0024 RtcWakeAction::RtcWakeAction()
0025 {
0026     KLocalizedString::setApplicationDomain(QByteArrayLiteral("kalarm"));
0027 }
0028 
0029 ActionReply RtcWakeAction::settimer(const QVariantMap& args)
0030 {
0031     qint64 t = args[QStringLiteral("time")].toLongLong();
0032     qCDebug(KALARM_LOG) << "RtcWakeAction::settimer(" << t << ")";
0033 
0034     // Find the rtcwake executable
0035     QString exe(QStringLiteral("/usr/sbin/rtcwake"));   // default location
0036     FILE* wh = popen("whereis -b rtcwake", "r");
0037     if (wh)
0038     {
0039         char buff[512] = { '\0' };
0040         bool ok = fgets(buff, sizeof(buff), wh);
0041         pclose(wh);
0042         if (ok)
0043         {
0044             // The string should be in the form "rtcwake: /path/rtcwake"
0045             char* start = strchr(buff, ':');
0046             if (start)
0047             {
0048                 if (*++start == ' ')
0049                     ++start;
0050                 char* end = strpbrk(start, " \r\n");
0051                 if (end)
0052                     *end = 0;
0053                 if (*start)
0054                 {
0055                     exe = QString::fromLocal8Bit(start);
0056                     qCDebug(KALARM_LOG) << "RtcWakeAction::settimer:" << exe;
0057                 }
0058             }
0059         }
0060     }
0061 
0062     // Set the wakeup by executing the rtcwake command
0063     int result = -2;   // default = command not found
0064     QProcess proc;
0065     if (!exe.isEmpty())
0066     {
0067         // The wakeup time is set using a time from now ("-s") in preference to
0068         // an absolute time ("-t") so that if the hardware clock is not in sync
0069         // with the system clock, the alarm will still occur at the correct time.
0070         // The "-m no" option sets the wakeup time without suspending the computer.
0071 
0072         // If 't' is zero, the current wakeup is cancelled by setting a new wakeup
0073         // time 2 seconds from now, which will then expire.
0074         qint64 now = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
0075         proc.setProgram(exe);
0076         proc.setArguments({ QStringLiteral("-m"), QStringLiteral("no"), QStringLiteral("-s"), QString::number(t ? t - now : 2) });
0077         proc.start();
0078         proc.waitForStarted(5000); // allow a timeout of 5 seconds
0079         result = proc.exitCode();
0080     }
0081     QString errmsg;
0082     switch (result)
0083     {
0084         case 0:
0085             return ActionReply::SuccessReply();
0086         case -2:
0087             errmsg = xi18nc("@text/plain", "Could not run <command>%1</command> to set wake from suspend", QStringLiteral("rtcwake"));
0088             break;
0089         default:
0090             errmsg = xi18nc("@text/plain", "Error setting wake from suspend.<nl/>Command was: <command>%1 %2</command><nl/>Error code: %3.", proc.program(), proc.arguments().join(QLatin1Char(' ')), result);
0091             break;
0092     }
0093     ActionReply reply = ActionReply::HelperErrorReply(result);
0094     reply.setErrorDescription(errmsg);
0095     qCDebug(KALARM_LOG) << "RtcWakeAction::settimer: Code=" << reply.errorCode() << reply.errorDescription();
0096     return reply;
0097 }
0098 
0099 KAUTH_HELPER_MAIN("org.kde.kalarm.rtcwake", RtcWakeAction)   //NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
0100 
0101 #include "moc_rtcwakeaction.cpp"
0102 
0103 // vim: et sw=4: