s6

Mirror/fork of https://skarnet.org/software/s6/
git clone https://ccx.te2000.cz/git/s6
Log | Files | Refs | README | LICENSE

s6_supervise_unlink.c (1407B)


      1 /* ISC license. */
      2 
      3 #include <errno.h>
      4 #include <string.h>
      5 #include <unistd.h>
      6 #include <sys/stat.h>
      7 
      8 #include <skalibs/posixplz.h>
      9 #include <skalibs/allreadwrite.h>
     10 #include <skalibs/djbunix.h>
     11 
     12 #include <s6/supervise.h>
     13 
     14 void s6_supervise_unlink (char const *scdir, char const *name, uint32_t options)
     15 {
     16   int e = errno ;
     17   int fd = -1, fdlog = -1 ;
     18   size_t scdirlen = strlen(scdir) ;
     19   size_t namelen = strlen(name) ;
     20   char fn[scdirlen + namelen + sizeof(S6_SUPERVISE_CTLDIR) + 14] ;
     21   memcpy(fn, scdir, scdirlen) ;
     22   fn[scdirlen] = '/' ;
     23   memcpy(fn + scdirlen + 1, name, namelen) ;
     24   if (options & 4)
     25   {
     26     memcpy(fn + scdirlen + 1 + namelen, "/down", 6) ;
     27     unlink_void(fn) ;
     28   }
     29   if (options & 1)
     30   {
     31     memcpy(fn + scdirlen + 1 + namelen, "/" S6_SUPERVISE_CTLDIR, sizeof(S6_SUPERVISE_CTLDIR)) ;
     32     memcpy(fn + scdirlen + 1 + namelen + sizeof(S6_SUPERVISE_CTLDIR), "/control", 9) ;
     33     fd = open_write(fn) ;
     34     memcpy(fn + scdirlen + 1 + namelen, "/log/" S6_SUPERVISE_CTLDIR, 4 + sizeof(S6_SUPERVISE_CTLDIR)) ;
     35     memcpy(fn + scdirlen + 5 + namelen + sizeof(S6_SUPERVISE_CTLDIR), "/control", 9) ;
     36     fdlog = open_write(fn) ;
     37   }
     38   
     39   fn[scdirlen + 1 + namelen] = 0 ;
     40   unlink_void(fn) ;
     41   if (fd >= 0)
     42   {
     43     fd_write(fd, "xd", 1 + !!(options & 2)) ;
     44     fd_close(fd) ;
     45   }
     46   if (fdlog >= 0)
     47   {
     48     fd_write(fdlog, "xo", 1 + !!(options & 2)) ;
     49     fd_close(fdlog) ;
     50   }
     51   errno = e ;
     52 }