aboutsummaryrefslogtreecommitdiffstats
path: root/src/lockfile.c
blob: 9dd292554c7fea557ea765096d5e5b4b00088a11 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * lockfile.c:
 *
 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
 * All rights reserved.
 *
 */

static char rcsid[] = "$Id$";

/*
 * $Log$
 * Revision 1.2  2008/02/15 16:48:56  james
 * *** empty log message ***
 *
 * Revision 1.1  2008/02/15 15:09:17  james
 * *** empty log message ***
 *
 */

#define LOCK_ASCII
#undef LOCK_BINARY


Filelist *filelist_new(void)
{



}


static int
chown_uucp (fd)
     int fd;
{
  static int uuid = -1, ugid;
  struct passwd *pw;

  if (uuid < 0)
    {
      if (pw = getpwnam ("uucp"))
        {
          uuid = pw->pw_uid;
          ugid = pw->pw_gid;
        }
      else
        {
          return -1;
        }
    }
  return fchown (fd, uuid, ugid);
}

int
lockfile_make
{
  char buf[1024], tmpfn[1024];
  char *ptr;
  int fd;
  int i;

  strcpy (tmpfn, name);

  ptr = rindex (tmpfn, '/');
  if (!ptr)
    return -1;

  ptr++;

  ptr += sprintf (ptr, "LTMP.%d", getpid ());
  *ptr = 0;

  i = sprintf (buf, "%10d\n", getpid ());

  fd = open (tmpfn, O_WRONLY | O_CREAT | O_TRUNC, 0444);
  if (fd < 0)
    {
      unlink (tmpfn);
      return -1;
    }

  write (fd, buf, i);
  fchmod (fd, 044);
  if (chown_uucp (fd))
    {
      close (fd);
      unlink (tmpfn);
      return -1;
    }

  close (fd);

  if (link (tmpfn, name) < 0)
    {
      unlink (tmpfn);
      return -1;
    }

  unlink (tmpfn);
  return 0;
}



void
lockfile_add_name_from_path (File_list *fl,char *ptr)
{
  printf ("lock by file %s\n", ptr);
}

void
lockfile_add_name_from_dev (File_list *fl,dev_t dev)
{
  printf ("lock by dev %x\n", dev);
}

void lockfile_check_dir_for_dev(File_list *fl,char *dir,dev_t dev)
{
  char buf[1024];
  struct stat ent_stat;
  struct dirent *de;
  DIR *d;

  for (d = opendir (DEV); (de = readdir (d));)
    {
      strcpy (buf, DEV);
      strcat (buf, de->d_name);

      if (stat (buf, &ent_stat))
        continue;
      if (!S_ISCHR (ent_stat.st_mode))
        continue;
      if (ent_stat.st_rdev != dev)
        continue;

      lockfile_add_name_from_path (fl,buf);

    }
  closedir (d);
}

File_list *
construct_possible_lock_files (char *device)
{
  struct stat dev_stat;
  File_list *ret=NULL;


  if (stat (device, &dev_stat))
    return ret;
  if (!S_ISCHR (dev_stat.st_mode))
    return ret;

  ret=filelist_new();

  lockfile_add_name_from_dev (ret,dev_stat.st_rdev);

  lockfile_add_name_from_path (ret,device);

  lockfile_check_dir_for_dev(ret,"/dev/",dev_stat.st_rdev);
  lockfile_check_dir_for_dev(ret,"/dev/usb/",dev_stat.st_rdev);
  lockfile_check_dir_for_dev(ret,"/dev/tts/",dev_stat.st_rdev);

  return ret;
}