I am writing a simple C application running on a Raspberry Pi that uses D2XX drivers to communicate with a serial port device. I followed a number of online guides and reference guides to get it working, and took steps such as setting custom udev rules to ensure drivers load correctly, I followed the FTDI build instructions to set up a shared library, I use -lthe gcc argument to communicate in the library when compiling, and I run my C program using sudoto ensure that the drivers are correctly accessed. And it was successful! The program works as intended.
Now I am trying to convert my simple program into a daemon process, which can be controlled using init.d script (a la service start) and ran into difficulties.
For simplicity, here is an irrigated version of my C program that works :
myprog.c:
#include <stdlib.h>
#include "ftd2xx.h"
int main(int argc, char *argv[])
{
DWORD i, iNumDevs = 0;
char *serialNumber = malloc(64);
FT_STATUS ftStatus = FT_CreateDeviceInfoList(&iNumDevs);
for (i = 0; i < iNumDevs; i++) {
ftStatus = FT_ListDevices((PVOID)i, serialNumber, FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_NUMBER);
if (FT_OK == ftStatus) {
break;
}
}
return EXIT_SUCCESS;
}
I will compile this with gcc -lftd2xx -o myprog myprog.cand then run it with sudo ./myprogand honestly do everything that it should do. But now, when I try to remake the same code into a daemon, I am following some other online tutorials, and the code above has been converted to something that looks more like this. Currently this one does not work:
mydaemon.c:
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "ftd2xx.h"
int main(int argc, char *argv[])
{
pid_t pid, sid;
pid = fork();
if (pid < 0) {
return EXIT_FAILURE;
}
if (pid > 0) {
return EXIT_SUCCESS;
}
umask(0);
openlog("mydaemon", LOG_PID|LOG_CONS, LOG_USER);
sid = setsid();
if (sid < 0) {
syslog(LOG_ERR, "Failed to set session ID on child process");
return EXIT_FAILURE;
}
if ((chdir("/")) < 0) {
syslog(LOG_ERR, "Failed to change working directory");
return EXIT_FAILURE;
}
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
while (1) {
DWORD i, iNumDevs = 0;
char *serialNumber = malloc(64);
syslog(LOG_INFO, "I get to this line");
FT_STATUS ftStatus = FT_CreateDeviceInfoList(&iNumDevs);
syslog(LOG_INFO, "I do not get to this line :( ");
sleep(10);
}
return EXIT_SUCCESS;
}
: gcc -lftd2xx -o mydaemon mydaemon.c; : sudo ./mydaemon, , , . /var/log/messages, , (.. " " ), . , , . .
, D2XX , . ? , , , ? , D2XX; , .