s6

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

s6_svc_writectl.c (1050B)


      1 /* ISC license. */
      2 
      3 #include <skalibs/sysdeps.h>
      4 #include <sys/stat.h>
      5 #include <fcntl.h>
      6 #include <errno.h>
      7 #include <string.h>
      8 #include <skalibs/djbunix.h>
      9 #include <skalibs/unix-transactional.h>
     10 #include <s6/supervise.h>
     11 
     12 int s6_svc_writectl (char const *service, char const *subdir, char const *s, size_t len)
     13 {
     14   size_t svlen = strlen(service) ;
     15   size_t sublen = strlen(subdir) ;
     16   int r ;
     17   char fn[svlen + sublen + 10] ;
     18   memcpy(fn, service, svlen) ;
     19   fn[svlen] = '/' ;
     20   memcpy(fn + svlen + 1, subdir, sublen) ;
     21   memcpy(fn + svlen + 1 + sublen, "/control", 9) ;
     22   r = s6_svc_write(fn, s, len) ;
     23   if (r != -2) return r ;
     24 
     25 #ifdef SKALIBS_HASODIRECTORY
     26 
     27  /* Investigate what went wrong */
     28 
     29   {
     30     int fdsub ;
     31     int fd = open2(service, O_RDONLY | O_DIRECTORY) ;
     32     if (fd == -1) return -1 ;
     33     fdsub = open2_at(fd, subdir, O_RDONLY | O_DIRECTORY) ;
     34     fd_close(fd) ;
     35     if (fdsub == -1) return (errno == ENOENT) ? 0 : -2 ;
     36     fd_close(fdsub) ;
     37     return -2 ;
     38   }
     39 
     40 #else
     41 
     42  /* Too bad, get a better system */
     43 
     44   return -2 ;
     45 
     46 #endif
     47 }