...
| Code Block |
|---|
|
if(!fgets(filename, sizeof(filename), stdiostdin)) {
/* handle error */
}
if(!open(filename, O_WRONLY, 0600)) {
/* handle error */
}
/* if filename is a fifo or a locked device the program may now hang in the open call */
|
...
| Code Block |
|---|
|
struct stat pre_s;
struct stat post_s;
int fildes;
if(!fgets(filename, sizeof(filename), stdiostdin)) {
/* handle error */
}
if((stat(filename, &pre_s) != 0) || (!S_ISREG(s.st_mode))) {
/* handle error */
}
/* due to a race condition here, we will verify with fstat later */
if(!(fildes = open(filename, O_WRONLY, 0600))) {
/* handle error */
}
if(!fstat(fildes, &post_s) {
/* handle error */
}
if(!(pre_s.st_mode == post_s.st_mode &&
pre_s.st_ino == post_s.st_ino &&
pre_s.st_dev == post_s.st_dev)) {
/* handle error */
}
/* operate on file */
|
Compliant Solution (Windows)
In the compliant solution, the log file is only opened once upon program startup, and is closed upon program termination. The log_message() function only writes the message to the already opened The GetFileType() function can be used to determine if the file is a disk file.
| Code Block |
|---|
|
HANDLE hFile = CreateFile(pFullPathName,
0, 0, NULL, OPEN_EXISTING, 0, NULL
);
if (hFile == INVALID_HANDLE_VALUE) {
/* handle error */
}
if (GetFileType(hFile) != FILE_TYPE_DISK) {
/* handle error */
}
|
...