ports/access.c: Use EACCES, not EPERM
PATCH 1/3 ports/access.c: Use EACCES, not EPERM
The man page for access() does not allow for a return value of EPERM, should be EACCES. Fix.
--- ports/unix/guts/access.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ports/unix/guts/access.c +1 -1
@@ -21,7 +21,7 @@if (buf.st_mode &0111 ) {return 0 ; }else { - errno =EPERM ; + errno =EACCES ;return -1 ; } }else {
Signature · 1 line
2.27.0
PATCH 2/3 ports/unix: Add faccessat and faccessat2
Add wrappers for the faccessat and faccessat2 glibc functions matching the way access.c works. faccessat2 was added in glibc 2.33.
--- ports/unix/guts/faccessat.c | 15 ++++++++++++ ports/unix/guts/faccessat2.c | 46 ++++++++++++++++++++++++++++++++++++ ports/unix/wrapfuncs.in | 2 ++ 3 files changed, 63 insertions(+) create mode 100644 ports/unix/guts/faccessat.c create mode 100644 ports/unix/guts/faccessat2.c
ports/unix/guts/faccessat.c new file +15 -0
@@ -0,0 +1,15 @@ +/* +* Copyright (c) 2021, Linux Foundation; see +* guts/COPYRIGHT for information. +* +* SPDX-License-Identifier: LGPL-2.1-only +* +* static int +* wrap_fsaccessat(int dirfd, const char *path, int mode, int flags) { +* int rc = -1; +*/ + rc =wrap_faccessat2 (dirfd, path, mode, flags); + +/* return rc; +* } +*/
ports/unix/guts/faccessat2.c new file +46 -0
46 lines not on this page · raw message
ports/unix/wrapfuncs.in +2 -0
@@ -10,6 +10,8 @@ int dup(int fd); /* noignore_path=1 */ int chdir(const char *path); /* noignore_path=1 */ int fchdir(int dirfd); /* noignore_path=1 */ int access(const char *path, int mode); +int faccessat(int dirfd, const char *path, int mode, int flags); +int faccessat2(int dirfd, const char *path, int mode, int flags); FTS *fts_open(char * const *path_argv, int options, int (*compar)(const FTSENT **, const FTSENT **)); /* inode64=1 */ int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), int nopenfd); int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int nopenfd, int flag);
Signature · 1 line
2.27.0
PATCH 3/3 ports/rename/renameat: Avoid race when renaming files
In reply to Richard PurdieWe're seeing failures where a file is renamed under pseudo but an access appears to be made to the old filename before the OP_RENAME has hit the database but after the real_rename has applied in the kernel.
This is effectively the MAY_UNLINK problem for the original filename. There were protections for the newpath but not the oldpath.
To try and avoid these aborts(), mark the original path as MAY_UNLINK however we need to be careful not to trigger the DID_UNLINK but instead update the deleting entry in the database as the rename completes. To do this, we no clear the deleting flag during the database rename operation in SQL.
Also, we have to stop removing the by_ino matches where by_ino.deleting is set, else we may lose the renamed file's attributes that way.
--- ports/unix/guts/rename.c | 35 ++++++++++++++++++++++------------- ports/unix/guts/renameat.c | 34 +++++++++++++++++++++------------- pseudo.c | 11 +++++------ pseudo_db.c | 4 ++-- 4 files changed, 50 insertions(+), 34 deletions(-)
ports/unix/guts/rename.c +22 -13
@@ -13,7 +13,8 @@int oldrc, newrc;int save_errno;int old_db_entry =0 ; -int may_unlinked =0 ; +int may_unlink_new =0 ; +int may_unlink_old =0 ; pseudo_debug(PDBGF_OP, "rename: %s->%s\n", oldpath ? oldpath : "<nil>",
@@ -43,28 +44,36 @@/* as with unlink, we have to mark that the file may get deleted */ msg =pseudo_client_op (OP_MAY_UNLINK ,0 ,-1 ,-1 , newpath, newrc ?NULL : &newbuf);if (msg && msg->result ==RESULT_SUCCEED ) - may_unlinked =1 ; + may_unlink_new =1 ; +/* oldpath is also likely to disappear. Something could call stat() after +real_rename so we need to mark as MAY_UNLINK too */ + msg =pseudo_client_op (OP_MAY_UNLINK ,0 ,-1 ,-1 , oldpath, oldrc ?NULL : &oldbuf); +if (msg && msg->result ==RESULT_SUCCEED ) + may_unlink_old =1 ; + msg =pseudo_client_op (OP_STAT ,0 ,-1 ,-1 , oldpath, oldrc ?NULL : &oldbuf);if (msg && msg->result ==RESULT_SUCCEED ) old_db_entry =1 ; rc =real_rename (oldpath, newpath); save_errno = errno; -if (may_unlinked) { -if (rc ==-1 ) { -/* since we failed, that wasn't really unlinked -- put -* it back. -*/ -pseudo_client_op (OP_CANCEL_UNLINK ,0 ,-1 ,-1 , newpath, &newbuf); - }else { -/* confirm that the file was removed */ -pseudo_client_op (OP_DID_UNLINK ,0 ,-1 ,-1 , newpath, &newbuf); - } - } +if (rc ==-1 ) { +/* since we failed, that wasn't really unlinked -- put +* it back. +*/ +if (may_unlink_new) +pseudo_client_op (OP_CANCEL_UNLINK ,0 ,-1 ,-1 , newpath, &newbuf); +if (may_unlink_old) +pseudo_client_op (OP_CANCEL_UNLINK ,0 ,-1 ,-1 , oldpath, &oldbuf);/* and we're done. */ errno = save_errno;return rc; } + +/* confirm that the file was removed */ +if (may_unlink_new) +pseudo_client_op (OP_DID_UNLINK ,0 ,-1 ,-1 , newpath, &newbuf); +/* OP_DID_UNLINK for oldpath is handled by the server */ save_errno = errno;/* rename(3) is not mv(1). rename(file, dir) fails; you must provide
ports/unix/guts/renameat.c +21 -13
@@ -13,7 +13,8 @@int oldrc, newrc;int save_errno;int old_db_entry =0 ; -int may_unlinked =0 ; +int may_unlink_new =0 ; +int may_unlink_old =0 ; pseudo_debug(PDBGF_FILE, "renameat: %d,%s->%d,%s\n", olddirfd, oldpath ? oldpath : "<nil>",
@@ -55,29 +56,36 @@/* as with unlink, we have to mark that the file may get deleted */ msg =pseudo_client_op (OP_MAY_UNLINK ,0 ,-1 , newdirfd, newpath, newrc ?NULL : &newbuf);if (msg && msg->result ==RESULT_SUCCEED ) - may_unlinked =1 ; + may_unlink_new =1 ; +/* oldpath is also likely to disappear. Something could call stat() after +real_rename so we need to mark as MAY_UNLINK too */ + msg =pseudo_client_op (OP_MAY_UNLINK ,0 ,-1 , olddirfd, oldpath, oldrc ?NULL : &oldbuf); +if (msg && msg->result ==RESULT_SUCCEED ) + may_unlink_old =1 ; + msg =pseudo_client_op (OP_STAT ,0 ,-1 , olddirfd, oldpath, oldrc ?NULL : &oldbuf);if (msg && msg->result ==RESULT_SUCCEED ) old_db_entry =1 ; rc =real_renameat (olddirfd, oldpath, newdirfd, newpath); save_errno = errno; -if (may_unlinked) { -if (rc ==-1 ) { -/* since we failed, that wasn't really unlinked -- put -* it back. -*/ -pseudo_client_op (OP_CANCEL_UNLINK ,0 ,-1 , newdirfd, newpath, &newbuf); - }else { -/* confirm that the file was removed */ -pseudo_client_op (OP_DID_UNLINK ,0 ,-1 , newdirfd, newpath, &newbuf); - } - }if (rc ==-1 ) { +/* since we failed, that wasn't really unlinked -- put +* it back. +*/ +if (may_unlink_new) +pseudo_client_op (OP_CANCEL_UNLINK ,0 ,-1 , newdirfd, newpath, &newbuf); +if (may_unlink_old) +pseudo_client_op (OP_CANCEL_UNLINK ,0 ,-1 , olddirfd, oldpath, &oldbuf);/* and we're done. */ errno = save_errno;return rc; } + +/* confirm that the file was removed */ +if (may_unlink_new) +pseudo_client_op (OP_DID_UNLINK ,0 ,-1 , newdirfd, newpath, &newbuf); +/* OP_DID_UNLINK for oldpath is handled by the server */ save_errno = errno;/* rename(3) is not mv(1). rename(file, dir) fails; you must provide
pseudo.c +5 -6
@@ -688,14 +688,13 @@ pseudo_op(pseudo_msg_t *msg, const char *program, const char *tag, char **responbreak ; }if (mismatch) { -/* a mismatch, but we were planning to delete -* the file, so it must have gotten deleted -* already. -*/ if (by_ino.deleting !=0 ) { -pseudo_debug (PDBGF_FILE ,"inode mismatch for '%s' -- old one was marked for deletion, deleting.\n" , +/* a mismatch, likely a race but we were planning to +* delete the file, or are in the middle of a rename +* so continue, need the old entry for a rename. +*/ +pseudo_debug (PDBGF_FILE ,"inode mismatch for '%s' -- old one was marked for deletion.\n" , msg->path); -pdb_did_unlink_file (path_by_ino, &by_ino, by_ino.deleting); }else { pseudo_diag("path mismatch [%d link%s]: ino %llu db '%s' req '%s'.\n", msg->nlink,
pseudo_db.c +2 -2
@@ -2020,8 +2020,8 @@ int pdb_rename_file(const char *oldpath, pseudo_msg_t *msg) { static sqlite3_stmt *update_exact, *update_sub;int rc; -char *sql_update_exact ="UPDATE files SET path = ? WHERE path = ?;" ; -char *sql_update_sub ="UPDATE files SET path = replace(path, ?, ?) " +char *sql_update_exact ="UPDATE files SET path = ?, deleting = 0 WHERE path = ?;" ; +char *sql_update_sub ="UPDATE files SET path = replace(path, ?, ?), deleting = 0 " "WHERE (path > (? || '/') AND path < (? || '0'));" ;if (!file_db &&get_dbs ()) {
Signature · 1 line
2.27.0