ax25d and STDIN
I am trying to write a simple scripts in PHP to run with ax25d. It will be used to collect information from aid stations at the upcoming Marine Corps Marathon in Washington, D.C.
I didn't want to get into network programming if it isn't necessary. And I would prefer to use an uncompiled language, like php or perl, so that I can make changes quickly if necessary.
To output data I am using fopen("php://stdout", "w"); and to read data I am using fopen("php://stdin", "r"); The writing data part works fine, but whenever I attempt to read in data from stdin, ax25d hangs up.
I have noticed in C++ I get this same problem when I attempt to use scanf to read in. However, if I use cin instead, it works fine.
Does anybody know why this occurs and what I might do about it?
Thanks Very Much!
73 de Ben, KF4KJQ
Quoted 3 lines · Ben Gelb
To output data I am using
fopen("php://stdout", "w"); and to read data I am usingfopen("php://stdin", "r"); The writing data part works fine, but whenever I attempt to read in data from stdin, ax25d hangs up.
What do you mean by ax25d hanging up?
After spawning a program ax25d doesn't actively participate in the communication between the program and the connectee. To spawn a program it simply does a fork() followed by an exec(). The programs stdin/out/err are redirected to point at the incoming connection socket (dup2()) before the exec call.
What you may be seeing is the side effect of AX.25 sockets being SOCK_SEQPACKET sockets. This means you always have to read a full packet when reading. If you read less than a full packet worth of data, the rest of the packet is thrown away. In practise you have no way of knowing how big packet there might be waiting in the kernel buffers so you will have to do a read() with a buffer "big enough"...
The standard IO libraries do various amounts of buffering if you use stdio functions to read so that might explain why something works and something else does not.
Then again you might be having some completely another problem with your program... :)
Signature · 3 lines
Tomi Manninen Internet: [email protected] OH2BNS AX.25: [email protected] KP20ME04 Amprnet: [email protected]
When I say 'hangs up' I mean that the user is disconnected from the ax25 link. I should have been more specific. For some reason, the link dies.
I was using fgets to read data. I tried using fread with length of 1024, 2048, and 10000. It was no different.
I doubt the problem is in my program. It basically asks the user for a line of input, which it them spits back out again. It is simply a test program. It works fine on the console (yes, I remembered to change the return character to '\r' when I run it with ax25d).
I am starting to sense there may not be an easy solution to this problem.
Thanks for answering so quickly!
73 Ben
Quoted 37 lines · On Fri, 2002-09-27 at 03:52, Tomi Manninen OH2BNS wrote:
Quoted 3 lines · Ben Gelb
To output data I am using
fopen("php://stdout", "w"); and to read data I am usingfopen("php://stdin", "r"); The writing data part works fine, but whenever I attempt to read in data from stdin, ax25d hangs up.What do you mean by ax25d hanging up?
After spawning a program ax25d doesn't actively participate in the communication between the program and the connectee. To spawn a program it simply does a
fork()followed by anexec(). The programs stdin/out/err are redirected to point at the incoming connection socket (dup2()) before the exec call.What you may be seeing is the side effect of AX.25 sockets being
SOCK_SEQPACKETsockets. This means you always have to read a full packet when reading. If you read less than a full packet worth of data, the rest of the packet is thrown away. In practise you have no way of knowing how big packet there might be waiting in the kernel buffers so you will have to do aread()with a buffer "big enough"...The standard IO libraries do various amounts of buffering if you use stdio functions to read so that might explain why something works and something else does not.
Then again you might be having some completely another problem with your program... :)
Signature · 8 lines
Tomi Manninen Internet: [email protected] OH2BNS AX.25: [email protected] KP20ME04 Amprnet: [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-hams" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Hi again Ben,
Quoted 4 lines
When I say 'hangs up' I mean that the user is disconnected from the ax25 link. I should have been more specific. For some reason, the link dies. I was using fgets to read data. I tried using fread with length of 1024, 2048, and 10000. It was no different.
Ok, so it is probably not the SEQPACKET problem either. However I really can't think of any other explanation than a problem with your program.
As I said when someone connects to your system, ax25d accept()s the connection, then fork()s. The parent process (ax25d) closes the socket belonging to the incoming connection thereby completely detaching itself from it. Ax25d then continues as if it never knew about that connection.
The child process copies the incoming connecting socket to it's stdin/out/err and then exec()s your program. After that your program has full control over the connection. Only your program can close the connection.
Well, there is another possibility and that is that the kernel closes the connection for some reason. I think some misconfiguration of the kernel ax25 support could cause that to happen.
Anyway my first guess still is that there is something wrong with your program. Are you absolutely 100% sure that ax25d manages to start it? Could the read fail for some other reason? Have you tried logging exactly what fails? I recommend making your program log each and every possible error situation somewhere, maybe the system log.
Signature · 3 lines
Tomi Manninen Internet: [email protected] OH2BNS AX.25: [email protected] KP20ME04 Amprnet: [email protected]
Thanks for helping me with this.
I have made some progress.
I have replaced the line where I originally tried to use fgets, with this wierd hacky solution:
while(1) {
$c = fread($in, 1);
if($c == $RETURN_CHAR) break;
$in_string.= $c;
}The program works as intended now.
Using fread with a large length doesn't work, because EOF is never found in stdin. So it just keeps waiting for the number of bytes specified.
I don't really understand why this works with the SEQPACKET consideration. I am definitely not getting the whole packet at once, yet, as I said, it does work.
Does this make any sense to you?
Thanks for you help!
73 Ben
Quoted 39 lines · Tomi Manninen
Hi again Ben,
Quoted 4 lines
When I say 'hangs up' I mean that the user is disconnected from the ax25 link. I should have been more specific. For some reason, the link dies. I was using fgets to read data. I tried using fread with length of 1024, 2048, and 10000. It was no different.
Ok, so it is probably not the SEQPACKET problem either. However I really can't think of any other explanation than a problem with your program.
As I said when someone connects to your system, ax25d accept()s the connection, then fork()s. The parent process (ax25d) closes the socket belonging to the incoming connection thereby completely detaching itself from it. Ax25d then continues as if it never knew about that connection.
The child process copies the incoming connecting socket to it's stdin/out/err and then exec()s your program. After that your program has full control over the connection. Only your program can close the connection.
Well, there is another possibility and that is that the kernel closes the connection for some reason. I think some misconfiguration of the kernel ax25 support could cause that to happen.
Anyway my first guess still is that there is something wrong with your program. Are you absolutely 100% sure that ax25d manages to start it? Could the read fail for some other reason? Have you tried logging exactly what fails? I recommend making your program log each and every possible error situation somewhere, maybe the system log.
Signature · 8 lines
Tomi Manninen Internet: [email protected] OH2BNS AX.25: [email protected] KP20ME04 Amprnet: [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-hams" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 2 lines · Ben Gelb
Using fread with a large length doesn't work, because EOF is never found in stdin. So it just keeps waiting for the number of bytes specified.
Actually I Don't think it's EOF that is missed. You would expect to see EOF only when the user disconnects. It's probably that the io stream is fully buffered. Playing with the setbuf() family of functions (if available in php, perl or whatever you use, I don't know) might be a solution.
Quoted 3 lines
I don't really understand why this works with the SEQPACKET consideration. I am definitely not getting the whole packet at once, yet, as I said, it does work.
Your code (which I believe in some form uses the libc stdio library) buffers the reads and the actual low level read() is called with a buffer larger than what you specify. The default is probably the machine page size. I think that's why it works.
I have found that the safest option is to directly use a low level read() call and handle any buffering my self. I don't know if that is an option for you however. So after all your hack might the best solution.
Signature · 3 lines
Tomi Manninen Internet: [email protected] OH2BNS AX.25: [email protected] KP20ME04 Amprnet: [email protected]
I think I am going to stick with my hack. It works fine for the amount of data I will be inputting at a time (less than 80 chars) so I don't forsee any problems.
There is no real way to do a low-level read() in php, not that I am aware of anyway, and the setbuf() family of functions doesn't exist.
Thanks for your help, perhaps I'll consider using C next time, but I really want an uncompiled language so I can make changes quickly if problems arise. I think I mentioned that before.
73 Ben
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Tomi Manninen
Sent: Sunday, September 29, 2002 2:27 PM
To: Ben Gelb
Cc: [email protected]
Subject: Re: ax25d and STDIN
Quoted 1 line · Ben Gelb
Using fread with a large length doesn't work, because EOF is never
found
Quoted 1 line
in stdin. So it just keeps waiting for the number of bytes specified.
Actually I Don't think it's EOF that is missed. You would expect to see EOF only when the user disconnects. It's probably that the io stream is fully buffered. Playing with the setbuf() family of functions (if available in php, perl or whatever you use, I don't know) might be a solution.
Quoted 3 lines
I don't really understand why this works with the SEQPACKET consideration. I am definitely not getting the whole packet at once, yet, as I said, it does work.
Your code (which I believe in some form uses the libc stdio library) buffers the reads and the actual low level read() is called with a buffer
larger than what you specify. The default is probably the machine page size. I think that's why it works.
I have found that the safest option is to directly use a low level read()
call and handle any buffering my self. I don't know if that is an option
for you however. So after all your hack might the best solution.
Signature · 8 lines
Tomi Manninen Internet: [email protected] OH2BNS AX.25: [email protected] KP20ME04 Amprnet: [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-hams" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html