Closed ghost closed 8 years ago
it fails on test2/open_write:
curvecp/open_write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "open.h"
int open_write(const char *fn)
{
#ifdef O_CLOEXEC
return open(fn,O_CREAT | O_WRONLY | O_NONBLOCK | O_CLOEXEC,0644);
^^^^^^^^^^^
#else
int fd = open(fn,O_CREAT | O_WRONLY | O_NONBLOCK,0644);
if (fd == -1) return -1;
fcntl(fd,F_SETFD,1);
return fd;
#endif
}
opentest.c
/* test if close-on-exec works for open_write() */
static void test2(void) {
int fd;
close(1);
fd = open_write("opentest.data");
if (fd != 1) fail("unable to open opentest.data for writing");
^^^^^^^
cat();
}
...
int main(void) {
alarm(10);
run_mustfail(test1);
run_mustfail(test2);
^^^^^^^^^^^^^
run_mustfail(test3);
run_mustpass(testdummy);
_exit(0);
}
fix:
if (fd != -1) ? :)
^^
Hello, thanks for report. Can You describe how did You catch the problem (musl libc version, ...) ?
It looks like problem in libc.
it's neither musl libc nor tinyssh. it's cat utility from embutils
# ls
cat.c cat_embutils.c
# cat cat_embutils.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
static int open_write(const char *fn)
{
return open(fn, O_CREAT | O_WRONLY | O_NONBLOCK | O_CLOEXEC, 0644);
}
static void cat_embutils(void)
{
char *catcmd[2] = { (char *)"cat_embutils", 0 };
execvp(*catcmd, catcmd);
}
static void test_embutils(void)
{
int fd;
close(1);
fd = open_write("opentest_embutils.data");
if (fd != 1)
printf("unable to open opentest.data for writing");
cat_embutils();
}
int main()
{
test_embutils();
return 0 ;
}
# gcc cat.c -o cat_bb
# gcc cat_embutils.c -o cat_embutils
# ls -lh /bin/cat_embutils
-r-xr-xr-x 1 root root 9.0k May 28 12:57 /bin/cat_embutils
# ./cat_bb
cat: write error: Bad file descriptor
# echo $?
1
# ./cat_embutils
cat: -: short write
# echo $?
0
I dislike this type of errors :)
really, 'cat' from embutils doesn't correctly handle 'write' errors.
cat.c (line 85) from embutils-0.19: if (write(1,buf2,len2)<len2) { warn(filename,"short write"); break; }
.... time to rework the opentest without 'cat'
embutil's cat need only one line of code to fix it (res = 1;):
if (write(1,buf2,len2)<len2) {
warn(filename,"short write");
res = 1; /* return fase */
break;
}
}
if (fd) close(fd);
}
build.log: