Hi All,
I find an issue with macOS Mojave that a program runnning using launch daemon is unable to add entry using 'crontab' with popen()/system() API's. I even tried to direclty modifying "/usr/lib/cron/tabs/$userFile" from program (just for testing) and even it fails.
In case I run the same program from terminal, it works. Also if we disable SIP (system integrity protection) then also it is sucessfully able to add crontab entry.
Even if this program is run through popen() from another launch deamon process still the issue occurs.
Is Anyone else also sees the issue and is there a solution/workaround for the same ?
Here is the sample program.
#include <iostream>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <fstream>
#include <sys/signal.h>
bool terminateFlag = false;
//Signal handler for SIGTERM
void terminationHandler(int signal)
{
terminateFlag = true;
}
bool writeCron(std::string fileName, int uid)
{
bool retVal = true;
do
{
std::string temp = "/usr/bin/crontab -u ";
temp.append(getpwuid(uid)->pw_name);
temp.append(" ");
temp.append(fileName);
FILE *f = popen(temp.c_str(),"r");
if(f == NULL)
{
retVal = false;
}
int retval = pclose(f);
if (retval != 0)
{
retVal = false;
printf("%s - failed with error %d", __FUNCTION__, retval);
}
} while (false);
return retVal;
}
// NOTE - contents of crontemp file is following => 21 22 22 7 * /usr/local/test1/test2/TaskManager 67 >> /dev/null 2>&1
int main(int argc, const char * argv[]) {
signal(SIGINT, terminationHandler);
bool retVal = writeCron("./crontemp.txt", 0);
if (retVal)
{
exit(0);
}
else
{
while(!terminateFlag)
{
sleep(1);
}
}
return 0;
}