s6

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

s6-instance-delete.c (1597B)


      1 /* ISC license. */
      2 
      3 #include <stdint.h>
      4 #include <string.h>
      5 
      6 #include <skalibs/bytestr.h>
      7 #include <skalibs/types.h>
      8 #include <skalibs/sgetopt.h>
      9 #include <skalibs/tai.h>
     10 #include <skalibs/strerr.h>
     11 #include <skalibs/djbunix.h>
     12 
     13 #include <s6/supervise.h>
     14 
     15 #define USAGE "s6-instance-delete [ -X ] [ -t timeout ] service instancename"
     16 #define dieusage() strerr_dieusage(100, USAGE)
     17 
     18 int main (int argc, char const *const *argv)
     19 {
     20   tain tto = TAIN_INFINITE_RELATIVE ;
     21   size_t namelen ;
     22   uint32_t options = 1 ;
     23   PROG = "s6-instance-delete" ;
     24   {
     25     unsigned int t = 0 ;
     26     subgetopt l = SUBGETOPT_ZERO ;
     27     for (;;)
     28     {
     29       int opt = subgetopt_r(argc, argv, "Xt:", &l) ;
     30       if (opt == -1) break ;
     31       switch (opt)
     32       {
     33         case 'X' : options &= ~1U ; break ;
     34         case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
     35         default : dieusage() ;
     36       }
     37     }
     38     argc -= l.ind ; argv += l.ind ;
     39     if (t) tain_from_millisecs(&tto, t) ;
     40   }
     41   if (argc < 2) dieusage() ;
     42 
     43   namelen = strlen(argv[1]) ;
     44   if (!argv[1][0] || argv[1][0] == '.' || byte_in(argv[1], namelen, " \t\f\r\n", 5) < namelen)
     45     strerr_dief1x(100, "invalid instance name") ;
     46   s6_instance_chdirservice(argv[0]) ;
     47 
     48   tain_now_set_stopwatch_g() ;
     49   tain_add_g(&tto, &tto) ;
     50 
     51   if (s6_supervise_unlink_names_g(".", argv + 1, 1, options, &tto) == -1)
     52     strerr_diefu4sys(111, "prepare deletion of instance ", argv[1], " of service ", argv[0]) ;
     53 
     54   {
     55     char fn[14 + namelen] ;
     56     memcpy(fn, "../instances/", 13) ;
     57     memcpy(fn + 13, argv[1], namelen + 1) ;
     58     rm_rf(fn) ;
     59   }
     60 
     61   return 0 ;
     62 }