File: snlockf.c

package info (click to toggle)
sn 0.3.8-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 976 kB
  • sloc: ansic: 9,255; sh: 467; makefile: 210
file content (39 lines) | stat: -rw-r--r-- 1,017 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright � 1998-2000 Harold Tay.
 * Copyright � 2000- Patrik R�dman.
 */

/*
 * lockf() a file, for shell scripts.
 */

#include <unistd.h>
#include <string.h>
#include <out.h>
#include <format.h>

static const char ver_ctrl_id[] = "$Id: snlockf.c 29 2004-04-24 23:02:38Z patrik $";

int main (int argc, char **argv)
{
   char *p;
   char buffer[40];

   progname = ((p = strrchr(argv[0], '/')) ? p + 1 : argv[0]);
   if (argc != 1)   /* FIXME: Usage string is never printed... */
      fail(1, "Usage: %s (no arguments or options)\n"
           "%s always locks (using lockf()) descriptor 3, which should be open for writing.",
           progname, progname);

   if (lockf(3, F_TLOCK, 0) == -1)
      _exit(2);

   formats(buffer, 40, "%d\n", getpid());   /* Tell the script what our pid is, so it can kill us */
   write(1, buffer, strlen(buffer));
   fsync(1);
   
   sleep(60 * 60);
   _exit(0);
}