]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/state.c
Fix a number of the easier compile warnings: unused variables,
[nfs-utils.git] / utils / statd / state.c
1 /*
2  * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
3  * Modified by Olaf Kirch, 1996.
4  * Modified by H.J. Lu, 1998.
5  *
6  * NSM for Linux.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <dirent.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include "statd.h"
21
22
23 /* 
24  * Most NSM's keep the status number in an ASCII file.  I'm keeping it
25  * as an int (4-byte binary) for now...
26  */
27 void
28 change_state (void)
29 {
30   int fd, size;
31   
32   if ((fd = open (SM_STAT_PATH, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) == -1)
33     die ("open (%s): %s", SM_STAT_PATH, strerror (errno));
34
35   if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
36     die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
37
38   if (size != 0 && size != sizeof MY_STATE) {
39     note (N_ERROR, "Error in status file format...correcting.");
40
41     if (close (fd) == -1)
42       die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
43
44     if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
45       die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
46   }
47   note (N_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
48
49   if (lseek (fd, 0, SEEK_SET) == -1)
50     die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
51
52   if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
53     die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
54
55   if (fsync (fd) == -1)
56     note (N_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
57
58   if (close (fd) == -1)
59     note (N_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
60
61   if (MY_NAME == NULL) {
62     char fullhost[SM_MAXSTRLEN + 1];
63     struct hostent *hostinfo;
64
65     if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
66       die ("gethostname: %s", strerror (errno));
67
68     if ((hostinfo = gethostbyname (fullhost)) == NULL)
69       note (N_ERROR, "gethostbyname error for %s", fullhost);
70     else {
71       strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
72       fullhost[sizeof (fullhost) - 1] = '\0';
73     }
74
75     MY_NAME = xstrdup (fullhost);
76   }
77 }
78
79
80 /* 
81  * Fairly traditional use of two directories for this.
82  */
83 void 
84 shuffle_dirs (void)
85 {
86   DIR *nld;
87   struct dirent *de;
88   struct stat st;
89   char *src, *dst;
90   int len1, len2, len;
91   
92   if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
93     die ("stat (%s): %s", SM_DIR, strerror (errno));
94
95   if (!S_ISDIR (st.st_mode))
96     if (mkdir (SM_DIR, S_IRWXU) == -1)
97       die ("mkdir (%s): %s", SM_DIR, strerror (errno));
98
99   memset (&st, 0, sizeof st);
100
101   if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
102     die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
103
104   if (!S_ISDIR (st.st_mode))
105     if (mkdir (SM_BAK_DIR, S_IRWXU) == -1)
106       die ("mkdir (%s): %s", SM_BAK_DIR, strerror (errno));
107
108   if (!(nld = opendir (SM_DIR)))
109     die ("opendir (%s): %s", SM_DIR, strerror (errno));
110
111   len1=strlen(SM_DIR);
112   len2=strlen(SM_BAK_DIR);
113   while ((de = readdir (nld))) {
114     if (de->d_name[0] == '.')
115       continue;
116     len=strlen(de->d_name);
117     src=xmalloc(len1+len+2);
118     dst=xmalloc(len2+len+2);
119     sprintf (src, "%s/%s", SM_DIR, de->d_name);
120     sprintf (dst, "%s/%s", SM_BAK_DIR, de->d_name);
121     if (rename (src, dst) == -1)
122       die ("rename (%s to %s): %s", SM_DIR, SM_BAK_DIR, strerror (errno));
123     free(src);
124     free(dst);
125   }
126   if (closedir (nld) == -1)
127     note (N_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));
128 }