Verwirrung des Verhaltens offen () mit o_creat | o_excl unter LinuxLinux

Linux verstehen
Anonymous
 Verwirrung des Verhaltens offen () mit o_creat | o_excl unter Linux

Post by Anonymous »

Ich arbeite an einem eingebetteten Linux-System (Kernel-5.10.188), wobei ADBD im Zielsystem ausgeführt wird.

Code: Select all

PS C:\Users\aa> adb push C:\test\open /data/
C:\test\open: 1 file pushed. 0.7 MB/s (17096 bytes in 0.023s)
< /code>
im Zielsystem, < /p>
# ls /data/open -l
-rw-rw-rw-    1 root     root         17096 Mar 11 10:28 /data/open
Die Funktion von handle_send_file () in File_Sync_Service.c lautet wie folgt (ich habe 3 Zeilen von Printf hinzugefügt)

Code: Select all

196 static int handle_send_file(int s, char *path, mode_t mode, char *buffer)
197 {
198     syncmsg msg;
199     unsigned int timestamp = 0;
200     int fd;
201
202     printf("XXXXXX %s, path: %s, mode: %o\n", __func__, path, mode);
203     fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
204     printf("XXXXX fd: %d, errno: %d\n", fd, errno);
205     if(fd < 0 && errno == ENOENT) {
206         mkdirs(path);
207         fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
208     }
209     printf("XXXXX fd: %d, errno: %d\n", fd, errno);
210     if(fd < 0 && errno == EEXIST) {
211         fd = adb_open_mode(path, O_WRONLY, mode);
212     }
213     if(fd < 0) {
214         if(fail_errno(s))
215             return -1;
216         fd = -1;
217     }
< /code>
Ich wurde verwirrt, nachdem ich die Codes gelesen hatte. < /p>
203     fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
204     printf("XXXXX fd: %d, errno: %d\n", fd, errno);
Ich denke, wenn die/data/open vorhanden ist, sollte Zeile 203 -1 mit ERRNO von eEXIST zurückgeben, und es sollte zu Zeile 211 gehen, um die Datei mit O_WRONLY zu öffnen. Aber es tat nicht!

Code: Select all

XXXXXX handle_send_file, path: /data/open, mode: 666
XXXXX fd: 17, errno: 0
XXXXX fd: 17, errno: 0
sync: waiting for command
Ich habe erneut vom PC gedrückt und Got.

Code: Select all

XXXXXX handle_send_file, path: /data/open, mode: 666
XXXXX fd: 17, errno: 0
XXXXX fd: 17, errno: 0
sync: waiting for command
Es wird kein Fehler zurückgegeben, wenn der/data/open vorhanden ist! Ist das richtig und erwartet?

Code: Select all

280 /*
281  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
282  * . (Alas, it is not as standard as we'd hoped!) So, if it's
283  * not already defined, then define it here.
284  */
285 #ifndef TEMP_FAILURE_RETRY
286 /* Used to retry syscalls that can return EINTR.  */
287 #define TEMP_FAILURE_RETRY(exp) ({         \
288     typeof (exp) _rc;                      \
289     do {                                   \
290         _rc = (exp);                       \
291     } while (_rc == -1 && errno == EINTR); \
292     _rc; })
293 #endif

342 static __inline__ int  adb_open_mode( const char*  pathname, int  options, int  mode )
343 {
344     return TEMP_FAILURE_RETRY( open( pathname, options, mode ) );
345 }
Ich denke, es wird geöffnet , korrigieren Sie mich, wenn ich falsch liege.

Code: Select all

#include 
#include 
#include 
#include 
#include 

int adb_open_mode(const char *path, int flags, mode_t mode) {
return open(path, flags, mode);
}

void fake_mkdirs(const char *path) {
printf("Creating directories for %s\n", path);
}

int main(int argc, char **argv)
{
const char *path = "/tmp/testfile";
/// mode_t mode = 0644;
mode_t mode = 0666;
int fd;

if (argc != 2) {
printf("Usage: %s filetoopen\n", argv[0]);
exit(0);
}

path = argv[1];
fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
printf("%s: %d, fd: %d, errno: %d\n", __func__, __LINE__, fd, errno);
if (fd < 0 && errno == ENOENT) {
fake_mkdirs(path);
fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
printf("%s: %d, fd: %d, errno: %d\n", __func__, __LINE__, fd, errno);
}
printf("%s: %d, fd: %d, errno: %d\n", __func__, __LINE__, fd, errno);
if (fd < 0 && errno == EEXIST) {
fd = adb_open_mode(path, O_WRONLY, mode);
}
printf("%s: %d, fd: %d, errno: %d\n", __func__, __LINE__, fd, errno);

if (fd < 0) {
perror("Failed to open file");
} else {
printf("File opened successfully with fd %d\n", fd);
close(fd);
}

return 0;
}
< /code>
Ich habe es ausgeführt und folgt. < /p>
# ./a.out /data/open
main: 32, fd: -1, errno: 17
main: 38, fd: -1, errno: 17
main: 42, fd: 3, errno: 17
File opened successfully with fd 3

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post