]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/statd/state.c
a6a4077718f21d66163ab21a85386f9138591e94
[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   extern short int restart;
32   
33   if ((fd = open (SM_STAT_PATH, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) == -1)
34     die ("open (%s): %s", SM_STAT_PATH, strerror (errno));
35
36   if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
37     die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
38
39   if (size != 0 && size != sizeof MY_STATE) {
40     note (N_ERROR, "Error in status file format...correcting.");
41
42     if (close (fd) == -1)
43       die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
44
45     if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
46       die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
47   }
48   note (N_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
49
50   if (lseek (fd, 0, SEEK_SET) == -1)
51     die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
52
53   if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
54     die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
55
56   if (fsync (fd) == -1)
57     note (N_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
58
59   if (close (fd) == -1)
60     note (N_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
61
62   if (MY_NAME == NULL) {
63     char fullhost[SM_MAXSTRLEN + 1];
64     struct hostent *hostinfo;
65
66     if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
67       die ("gethostname: %s", strerror (errno));
68
69     if ((hostinfo = gethostbyname (fullhost)) == NULL)
70       note (N_ERROR, "gethostbyname error for %s", fullhost);
71     else {
72       strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
73       fullhost[sizeof (fullhost) - 1] = '\0';
74     }
75
76     MY_NAME = xstrdup (fullhost);
77   }
78 }
79
80
81 /* 
82  * Fairly traditional use of two directories for this.
83  */
84 void 
85 shuffle_dirs (void)
86 {
87   DIR *nld;
88   struct dirent *de;
89   struct stat st;
90   char *src, *dst;
91   int len1, len2, len;
92   
93   if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
94     die ("stat (%s): %s", SM_DIR, strerror (errno));
95
96   if (!S_ISDIR (st.st_mode))
97     if (mkdir (SM_DIR, S_IRWXU) == -1)
98       die ("mkdir (%s): %s", SM_DIR, strerror (errno));
99
100   memset (&st, 0, sizeof st);
101
102   if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
103     die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
104
105   if (!S_ISDIR (st.st_mode))
106     if (mkdir (SM_BAK_DIR, S_IRWXU) == -1)
107       die ("mkdir (%s): %s", SM_BAK_DIR, strerror (errno));
108
109   if (!(nld = opendir (SM_DIR)))
110     die ("opendir (%s): %s", SM_DIR, strerror (errno));
111
112   len1=strlen(SM_DIR);
113   len2=strlen(SM_BAK_DIR);
114   while ((de = readdir (nld))) {
115     if (de->d_name[0] == '.')
116       continue;
117     len=strlen(de->d_name);
118     src=xmalloc(len1+len+2);
119     dst=xmalloc(len2+len+2);
120     sprintf (src, "%s/%s", SM_DIR, de->d_name);
121     sprintf (dst, "%s/%s", SM_BAK_DIR, de->d_name);
122     if (rename (src, dst) == -1)
123       die ("rename (%s to %s): %s", SM_DIR, SM_BAK_DIR, strerror (errno));
124     free(src);
125     free(dst);
126   }
127   if (closedir (nld) == -1)
128     note (N_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));
129 }