Skip to content

Commit d4d352d

Browse files
committed
Now properly records file before and after renaming.
No longer records the file if the rename system call failed. rename-to file is now also checked to see if the file has been processed before. Signed-off-by: Fotios Valasiadis <[email protected]>
1 parent 1054cd4 commit d4d352d

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

src/tracer.c

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -340,39 +340,45 @@ handle_execve(pid_t pid, PROCESS_INFO *pi, int dirfd, char *path)
340340
static void
341341
handle_rename_entry(pid_t pid, PROCESS_INFO *pi, int olddirfd, char *oldpath)
342342
{
343-
char *abspath = absolutepath(pid, olddirfd, oldpath);
344-
char *hash = get_file_hash(abspath);
345-
346-
FILE_INFO *f = find_finfo(abspath, hash);
347-
348-
if (!f) {
349-
f = next_finfo();
350-
finfo_new(f, oldpath, abspath, hash);
351-
record_file(f->outname, oldpath, abspath);
352-
record_hash(f->outname, f->hash);
353-
} else {
354-
free(oldpath);
355-
free(abspath);
356-
free(hash);
357-
}
358-
359-
pi->entry_info = (void *) (f - finfo);
360-
if (pi->entry_info == NULL)
361-
error(EXIT_FAILURE, errno, "on handle_rename_entry absolutepath");
343+
pi->entry_info = absolutepath(pid, olddirfd, oldpath);
344+
free(oldpath);
362345
}
363346

364347
static void
365-
handle_rename_exit(pid_t pid, PROCESS_INFO *pi, int newdirfd, char *newpath)
348+
handle_rename_exit(pid_t pid, PROCESS_INFO *pi, char *oldpath, int newdirfd,
349+
char *newpath)
366350
{
367-
FILE_INFO *from = finfo + (ptrdiff_t) pi->entry_info;
351+
char *oldabspath = pi->entry_info;
352+
char *newabspath = absolutepath(pid, newdirfd, newpath);
353+
354+
char *hash = get_file_hash(newabspath);
368355

369-
char *abspath = absolutepath(pid, newdirfd, newpath);
356+
FILE_INFO *from = find_finfo(oldabspath, hash);
370357

371-
FILE_INFO *to = next_finfo();
358+
if (!from) {
359+
from = next_finfo();
360+
finfo_new(from, oldpath, oldabspath, hash);
361+
record_file(from->outname, oldpath, oldabspath);
362+
record_hash(from->outname, hash);
363+
} else {
364+
free(oldpath);
365+
free(oldabspath);
366+
}
372367

373-
finfo_new(to, newpath, abspath, from->hash);
374-
record_file(to->outname, newpath, abspath);
375-
record_hash(to->outname, to->hash);
368+
FILE_INFO *to = find_finfo(newabspath, hash);
369+
370+
if (!to) {
371+
to = next_finfo();
372+
finfo_new(to, newpath, newabspath, hash);
373+
record_file(to->outname, newpath, newabspath);
374+
record_hash(to->outname, hash);
375+
} else {
376+
free(newpath);
377+
free(newabspath);
378+
if (from->hash != hash) {
379+
free(hash);
380+
}
381+
}
376382

377383
record_rename(pi->outname, from->outname, to->outname);
378384
}
@@ -447,6 +453,7 @@ handle_syscall_exit(pid_t pid, PROCESS_INFO *pi, int64_t rval)
447453
int flags;
448454
int dirfd;
449455
FILE_INFO *f;
456+
char *oldpath;
450457
int newdirfd;
451458
char *newpath;
452459

@@ -532,29 +539,32 @@ handle_syscall_exit(pid_t pid, PROCESS_INFO *pi, int64_t rval)
532539
#ifdef HAVE_SYS_RENAME
533540
case SYS_rename:
534541
// int rename(const char *oldpath, const char *newpath);
542+
oldpath = get_str_from_process(pid, (void *) pi->args[0]);
535543
newpath = get_str_from_process(pid, (void *) pi->args[1]);
536544

537-
handle_rename_exit(pid, pi, AT_FDCWD, newpath);
545+
handle_rename_exit(pid, pi, oldpath, AT_FDCWD, newpath);
538546
break;
539547
#endif
540548
#ifdef HAVE_SYS_RENAMEAT
541549
case SYS_renameat:
542550
// int renameat(int olddirfd, const char *oldpath, int newdirfd,
543551
// const char *newpath);
552+
oldpath = get_str_from_process(pid, (void *) pi->args[1]);
544553
newdirfd = pi->args[2];
545554
newpath = get_str_from_process(pid, (void *) pi->args[3]);
546555

547-
handle_rename_exit(pid, pi, newdirfd, newpath);
556+
handle_rename_exit(pid, pi, oldpath, newdirfd, newpath);
548557
break;
549558
#endif
550559
#ifdef HAVE_SYS_RENAMEAT2
551560
case SYS_renameat2:
552561
// int renameat2(int olddirfd, const char *oldpath, int newdirfd,
553562
// const char *newpath, unsigned int flags);
563+
oldpath = get_str_from_process(pid, (void *) pi->args[1]);
554564
newdirfd = pi->args[2];
555565
newpath = get_str_from_process(pid, (void *) pi->args[3]);
556566

557-
handle_rename_exit(pid, pi, newdirfd, newpath);
567+
handle_rename_exit(pid, pi, oldpath, newdirfd, newpath);
558568
break;
559569
#endif
560570
#ifdef HAVE_SYS_FORK

0 commit comments

Comments
 (0)