/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2014-06-08 00:13:06 UTC
  • Revision ID: teddy@recompile.se-20140608001306-ums93iqhkvztj7u1
Make mandos-client use unlinkat() instead of remove().

* plugins.d/mandos-client.d (main): Replace remove() with unlinkat().

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
 
                                   stdout, ferror(), remove() */
 
43
                                   stdout, ferror() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
57
57
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
58
58
                                   inet_pton(), connect(),
59
59
                                   getnameinfo() */
60
 
#include <fcntl.h>              /* open() */
 
60
#include <fcntl.h>              /* open(), unlinkat() */
61
61
#include <dirent.h>             /* opendir(), struct dirent, readdir()
62
62
                                 */
63
63
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
73
73
                                */
74
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
75
                                   getuid(), getgid(), seteuid(),
76
 
                                   setgid(), pause(), _exit() */
 
76
                                   setgid(), pause(), _exit(),
 
77
                                   unlinkat() */
77
78
#include <arpa/inet.h>          /* inet_pton(), htons() */
78
79
#include <iso646.h>             /* not, or, and */
79
80
#include <argp.h>               /* struct argp_option, error_t, struct
2617
2618
  /* Removes the GPGME temp directory and all files inside */
2618
2619
  if(tempdir != NULL){
2619
2620
    struct dirent **direntries = NULL;
2620
 
    struct dirent *direntry = NULL;
2621
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2622
 
                             alphasort);
2623
 
    if(numentries > 0){
2624
 
      for(int i = 0; i < numentries; i++){
2625
 
        direntry = direntries[i];
2626
 
        char *fullname = NULL;
2627
 
        ret = asprintf(&fullname, "%s/%s", tempdir,
2628
 
                       direntry->d_name);
2629
 
        if(ret < 0){
2630
 
          perror_plus("asprintf");
2631
 
          continue;
2632
 
        }
2633
 
        ret = remove(fullname);
2634
 
        if(ret == -1){
2635
 
          fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2636
 
                       strerror(errno));
2637
 
        }
2638
 
        free(fullname);
 
2621
    int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY));
 
2622
    if(tempdir_fd == -1){
 
2623
      perror_plus("open");
 
2624
    } else {
 
2625
      int numentries = scandir(tempdir, &direntries, notdotentries,
 
2626
                               alphasort);
 
2627
      if(numentries > 0){
 
2628
        for(int i = 0; i < numentries; i++){
 
2629
          ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
 
2630
          if(ret == -1){
 
2631
            fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
 
2632
                         " \"%s\", 0): %s\n", tempdir,
 
2633
                         direntries[i]->d_name, strerror(errno));
 
2634
          }
 
2635
        }
 
2636
        
 
2637
        /* need to clean even if 0 because man page doesn't specify */
 
2638
        free(direntries);
 
2639
        if(numentries == -1){
 
2640
          perror_plus("scandir");
 
2641
        }
 
2642
        ret = rmdir(tempdir);
 
2643
        if(ret == -1 and errno != ENOENT){
 
2644
          perror_plus("rmdir");
 
2645
        }
2639
2646
      }
2640
 
    }
2641
 
    
2642
 
    /* need to clean even if 0 because man page doesn't specify */
2643
 
    free(direntries);
2644
 
    if(numentries == -1){
2645
 
      perror_plus("scandir");
2646
 
    }
2647
 
    ret = rmdir(tempdir);
2648
 
    if(ret == -1 and errno != ENOENT){
2649
 
      perror_plus("rmdir");
 
2647
      TEMP_FAILURE_RETRY(close(tempdir_fd));
2650
2648
    }
2651
2649
  }
2652
2650