2 * Copyright (C) 1995-1997, 1999 Jeffrey A. Uphoff
3 * Modified by Olaf Kirch, 1996.
4 * Modified by H.J. Lu, 1998.
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...
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));
35 if ((size = read (fd, &MY_STATE, sizeof MY_STATE)) == -1)
36 die ("read (%s): %s", SM_STAT_PATH, strerror (errno));
38 if (size != 0 && size != sizeof MY_STATE) {
39 note (N_ERROR, "Error in status file format...correcting.");
42 die ("close (%s): %s", SM_STAT_PATH, strerror (errno));
44 if ((fd = creat (SM_STAT_PATH, S_IRUSR | S_IWUSR)) == -1)
45 die ("creat (%s): %s", SM_STAT_PATH, strerror (errno));
47 note (N_DEBUG, "New state: %u", (++MY_STATE % 2) ? MY_STATE : ++MY_STATE);
49 if (lseek (fd, 0, SEEK_SET) == -1)
50 die ("lseek (%s): %s", SM_STAT_PATH, strerror (errno));
52 if (write (fd, &MY_STATE, sizeof MY_STATE) != sizeof MY_STATE)
53 die ("write (%s): %s", SM_STAT_PATH, strerror (errno));
56 note (N_ERROR, "fsync (%s): %s", SM_STAT_PATH, strerror (errno));
59 note (N_ERROR, "close (%s): %s", SM_STAT_PATH, strerror (errno));
61 if (MY_NAME == NULL) {
62 char fullhost[SM_MAXSTRLEN + 1];
63 struct hostent *hostinfo;
65 if (gethostname (fullhost, SM_MAXSTRLEN) == -1)
66 die ("gethostname: %s", strerror (errno));
68 if ((hostinfo = gethostbyname (fullhost)) == NULL)
69 note (N_ERROR, "gethostbyname error for %s", fullhost);
71 strncpy (fullhost, hostinfo->h_name, sizeof (fullhost) - 1);
72 fullhost[sizeof (fullhost) - 1] = '\0';
75 MY_NAME = xstrdup (fullhost);
81 * Fairly traditional use of two directories for this.
92 if (stat (SM_DIR, &st) == -1 && errno != ENOENT)
93 die ("stat (%s): %s", SM_DIR, strerror (errno));
95 if (!S_ISDIR (st.st_mode))
96 if (mkdir (SM_DIR, S_IRWXU) == -1)
97 die ("mkdir (%s): %s", SM_DIR, strerror (errno));
99 memset (&st, 0, sizeof st);
101 if (stat (SM_BAK_DIR, &st) == -1 && errno != ENOENT)
102 die ("stat (%s): %s", SM_BAK_DIR, strerror (errno));
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));
108 if (!(nld = opendir (SM_DIR)))
109 die ("opendir (%s): %s", SM_DIR, strerror (errno));
112 len2=strlen(SM_BAK_DIR);
113 while ((de = readdir (nld))) {
114 if (de->d_name[0] == '.')
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));
126 if (closedir (nld) == -1)
127 note (N_ERROR, "closedir (%s): %s", SM_DIR, strerror (errno));