PDA

View Full Version : fifo files not working right?



Jeff Sadowski
14th October 2017, 06:38 AM
I setup password less ssh and made sure that was working
I made sure in the screen ssh gets wall messages


read_msgs()
{
echo reading $1
while [ 1 ];do
cat $1 > ~/.wall_messages.txt
while [ -f ~/.wall_lock ];do sleep 1;done
done
}

new_msgs()
{
echo "starting new_msgs"
while [ 1 ];do
sleep 1
touch ~/.wall_lock
pid=$(ps -fC cat|grep $1|awk '{print $2}')
kill ${pid}
sleep 1
MSGS=$(cat ~/.wall_messages.txt)
rm ~/.wall_lock
sleep 1
if [ "${MSGS}" != "" ];then
echo -e "${MSGS}"
fi
done
}

start()
{
echo starting
if [ "${LOGFILE}" = "" ];then
LOG_FILE=/tmp/wall_$(date "+%s").log
mkfifo ${LOG_FILE}
screen -d -m bash -c "ssh localhost > ${LOG_FILE}"
read_msgs ${LOG_FILE} 2>/dev/null &
new_msgs ${LOG_FILE} 2>/dev/null &
fi
}

case $1 in
stop)
pid=$(ps -fC cat|grep /tmp/wall_|awk '{print $2}')
if [ "$pid" != "" ];then kill -9 ${pid};fi
pid=$(ps -fC bash|grep /tmp/wall_|awk '{print $2}')
if [ "$pid" != "" ];then kill -9 ${pid};fi
pid=$(ps -ef|grep $(basename $0)|awk '{print $2}')
if [ "$pid" != "" ];then kill -9 ${pid};fi
rm -rf /tmp/wall_*
;;
*)
start &
;;
esac

I run this code and it starts off OK but after it reads the initial start it stops seeing anything else that should be going to the ssh session that is being directed to the fifo file

From everything I have read this should work
Am I not allowed to kill the old cat and start a new one?
Why does it stop reading the fifo file?
Or maybe my ssh stops writing to the fifo file?

My non fifo model is working but I truncate the logfile right after I read it but that leaves room for a miss that way. I was hoping using a fifo file I could guarantee I don't miss input.

ocratato
14th October 2017, 07:38 AM
My understanding of what is happening:

1. screen is starting the ssh program. This blocks until the fifo is opened for reading.
2. the cat in read_msgs() opens the fifo and starts reading from output of ssh. There is probably not much output from cat as it would buffer its input.
3. felicide (killing cat) which would abandon the content of the buffer.
4. I would expect ssh to stop with a broken pipe message since there is now nothing reading the fifo.

Jeff Sadowski
14th October 2017, 11:57 PM
My understanding of what is happening:

1. screen is starting the ssh program. This blocks until the fifo is opened for reading.
2. the cat in read_msgs() opens the fifo and starts reading from output of ssh. There is probably not much output from cat as it would buffer its input.
3. felicide (killing cat) which would abandon the content of the buffer.
4. I would expect ssh to stop with a broken pipe message since there is now nothing reading the fifo.

1.) I started with tee into the fifo ssh is working and screen is working. I thought that was the whole point of a fifo file so that you could read a bit at a time and it would buffer it?

2) I have seen cat used for fifo examples is there a better way? I chose cat because I thought it did not buffer.

3.) Why would cat loose a buffer when I shut it down nicely. It should finish writing any buffer it has and stop. I did not do a -9.

4.) As I answered for 1 it with tee I was still able to get in.

I thought of a way to do this without fifo and guarantee I don't miss wall data.