0

I'm writing some application code that is used to execute Linux shell commands, and it then logs the command details into an SQL database. This includes the output of STDOUT + STDERR (separately).

After the command has been executed, and assuming the process didn't output anything... could there be any reason to leave the STDOUT/STDERR fields as NULL -vs- setting them to be empty strings?

To put the question another way: is there technically any difference between these two things?

  • A process that doesn't output anything to STDOUT
  • A process that outputs an empty string to STDOUT (and nothing else)

And to put the question another way again... does it make sense to make these columns NOT NULL in SQL?

1

In a pipe (std-err/out) there is no concept of an empty string, it's just "no output";

> printf '' 
> printf '' | xxd

where as null is something

> printf '\0'
> printf '\0' | xxd
00000000: 00

The opposite is true in a database, and less is more, so have your scripts silent(empty string) and your DB empty(null).

0

Not sure I understand the question, but:

Writing to stdout is performing a:

write(1, memory_address, length)

Which writes length bytes stored at the memory_address to file descriptor 1 (1 for stdout, 2 for stderr). For instance, in echo test, echo (or the shell if echo is builtin) performs a write(1, "test\n", 5).

Though, it's a bit silly, you can call the write() system call with a length of 0.

With:

write(1, address, 0)

At least on Linux, the system call still checks that the file descriptor has been open in write or read+write mode, and that the address is a valid address (though it doesn't have to be readable). If stdout is a broken pipe, I don't see it causing a SIGPIPE signal delivery though.

So doing that of size zero is not strictly equivalent to not doing any write at all in that it could cause errors.

In practice, I find that most commands avoid the write() if they can.

I find that echo -n and printf '' don't do any write() system call in all the implementations I've tried. stdio functions (fputs()/printf()/fwrite()... don't perform any write() when you ask them to write an empty string).

To perform a 0-length write, you can try:

perl -e 'syswrite(STDOUT, "")'

Or

python -c 'import os; os.write(1, "")'

Which are raw interfaces in those interpreters to write().

Example:

$ strace -e write /bin/echo -n
$ strace -e write python -c 'import os; os.write(1, "")'
write(1, "", 0)                         = 0
$ python -c 'import os; os.write(1, "")' >&-
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
$ python -c 'import os; os.write(1, "")' 1< /dev/null
Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
$ printf '%s\n' '#include <unistd.h>' 'main(){write(1,(char*)-1,0);}' | strace -e write tcc -run -
write(1, "", 0)                         = -1 EFAULT (Bad address)
$ printf '%s\n' '#include <unistd.h>' 'main(){write(1,(char*)0,1);}' | strace -e write tcc -run -
write(1, NULL, 1)                       = -1 EFAULT (Bad address)
$ printf '%s\n' '#include <unistd.h>' 'main(){write(1,(char*)0,0);}' | strace -e write tcc -run -
write(1, NULL, 0)                       = 0
  • A write with length 0 is not silly if the fd is a datagram or seqpacket socket; it will send a packet of size 0 which could be used to signal an EOF, for instance (if you create 2 datagram sockets with socketpair(), closing one of them will not result in a read of length 0 (EOF) on the other). – mosvy Feb 5 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.