G
Gregory Graham
I have a temperature sensor device we use to shutdown our servers. It
relys on fopen() blocking when the serial port device isn't ready.
Unfortunately that's not what's happening. The fopen() returns
immediately without an error. What have I got wrong?
Basically when it gets too hot the device connects the 8 and 6 lines
to line 20. That should, as I understand it, tell the fopen() that
the file is available and cause the rest of the program to execute.
The code is below. I compile it with "gcc -o minmon minmon.c" and
invoke it as "minmon /dev/tyyS0" on SuSE9 with gcc 2.95.3
#include <stdio.h>
#include <signal.h>
int await_event(char* port)
{
FILE *file;
int fd;
if ((file = fopen(port, "r")) != NULL) /*blocks*/
fclose(file); /*no longer need port*/
else
{
fprintf(stderr, "ERROR opening port %s", port);
return -1;
}
return 0;
}
main (int argc, char* argv[])
{
int pid, getuid();
char* port, *script;
if (argc != 2 )
{
fprintf(stderr, "Usage:");
fprintf(stderr, "\t%s device\n", argv[0]);
exit(3);
}
else /*run in diagnostic mode*/
{
port = argv[1];
printf("watching on port: %s\n", port);
if (await_event(port) == 0) /*blocks*/
{
printf("%s: diagnostic power out\n", argv[0]);
/*insert shutdown code here*/
}
else
printf("%s: ERROR while waiting for event\n",
argv[0]);
exit(0);
}
}
relys on fopen() blocking when the serial port device isn't ready.
Unfortunately that's not what's happening. The fopen() returns
immediately without an error. What have I got wrong?
Basically when it gets too hot the device connects the 8 and 6 lines
to line 20. That should, as I understand it, tell the fopen() that
the file is available and cause the rest of the program to execute.
The code is below. I compile it with "gcc -o minmon minmon.c" and
invoke it as "minmon /dev/tyyS0" on SuSE9 with gcc 2.95.3
#include <stdio.h>
#include <signal.h>
int await_event(char* port)
{
FILE *file;
int fd;
if ((file = fopen(port, "r")) != NULL) /*blocks*/
fclose(file); /*no longer need port*/
else
{
fprintf(stderr, "ERROR opening port %s", port);
return -1;
}
return 0;
}
main (int argc, char* argv[])
{
int pid, getuid();
char* port, *script;
if (argc != 2 )
{
fprintf(stderr, "Usage:");
fprintf(stderr, "\t%s device\n", argv[0]);
exit(3);
}
else /*run in diagnostic mode*/
{
port = argv[1];
printf("watching on port: %s\n", port);
if (await_event(port) == 0) /*blocks*/
{
printf("%s: diagnostic power out\n", argv[0]);
/*insert shutdown code here*/
}
else
printf("%s: ERROR while waiting for event\n",
argv[0]);
exit(0);
}
}