echo in sh calls write function repeatedly in kernel driver?

Naeem Afzal naeem_jarral at
Wed Jun 24 21:03:42 UTC 2009

I used this sample echo driver listed here: and used Example 9-2 for 5.X FreeBSD. Modifed and added a printline in write function to display Count value:
I am using 7.1 FreeBSD version. I compiled the driver and ran the echo command, I see that driver's write function is being called 11 times if I use sh shell? Why is that? If I use bash, then write function is only called once which seems to be correct. Why sh buildin echo is send 11 writes? Any idea?

thanks & regards

# echo "1234" > /dev/echo
Opened device "echo" successfully.
Count value: 0
Count value: 1
Count value: 2
Count value: 3
Count value: 4
Count value: 5
Count value: 6
Count value: 7
Count value: 8
Count value: 9
Count value: 10
Closing device "echo."
# cat /dev/echo
Opened device "echo" successfully.
Closing device "echo."

Driver's write function is defined as:

static int
echo_write(struct cdev *dev, struct uio *uio, int ioflag)
    int err = 0;

   uprintf("Count value: %d\n",count);
    /* Copy the string in from user memory to kernel memory */
    err = copyin(uio->uio_iov->iov_base, echomsg->msg,
        MIN(uio->uio_iov->iov_len, BUFFERSIZE - 1));

    /* Now we need to null terminate, then record the length */
    *(echomsg->msg + MIN(uio->uio_iov->iov_len, BUFFERSIZE - 1)) = 0;
    echomsg->len = MIN(uio->uio_iov->iov_len, BUFFERSIZE);

    if (err != 0) {
        uprintf("Write failed: bad address!\n");

More information about the freebsd-questions mailing list