1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file nfs.c for the Moira Client, which allows users
6 * to quickly and easily maintain most parts of the Moira database.
7 * It Contains: All functions for manipulating NFS Physical directories.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
25 #include <moira_site.h>
28 #include "mit-copyright.h"
34 #define TYPE_NFS "NFS"
36 #define DEFAULT_DIR "/u1/lockers"
37 #define DEFAULT_DEVICE "/dev/ra1c"
38 #define DEFAULT_STATUS DEFAULT_YES /* active. */
39 #define DEFAULT_ALLOC "0"
40 #define DEFAULT_SIZE "0"
42 /* Function Name: UpdatePrint
43 * Description: store a useful string for updates to print.
44 * Arguments: info - info about NFS service stored in array of strings.
45 * Returns: useful string.
48 static char *UpdatePrint(char **info)
50 char temp_buf[BUFSIZ];
51 sprintf(temp_buf, "Machine %s Directory %s", info[NFS_NAME], info[NFS_DIR]);
52 return Strsave(temp_buf); /* Small memory leak here, but no good way
53 to avoid it that I see. */
56 /* Function Name: PrintNFSInfo
57 * Description: Prints NFS Physical service information.
58 * Arguments: info - the information.
59 * Returns: directory of this nfs server, for DeleteNFSService().
62 static char *PrintNFSInfo(char **info)
64 char buf[BUFSIZ], status_buf[BUFSIZ];
65 int status = atoi(info[NFS_STATUS]);
68 status_buf[0] = '\0'; /* clear string. */
70 if (status & MR_FS_STUDENT)
72 strcat(status_buf, "Student");
75 if (status & MR_FS_FACULTY)
78 strcat(status_buf, " and ");
79 strcat(status_buf, "Faculty");
82 if (status & MR_FS_STAFF)
85 strcat(status_buf, " and ");
86 strcat(status_buf, "Staff");
89 if (status & MR_FS_MISC)
92 strcat(status_buf, " and ");
93 strcat(status_buf, "Miscellaneous");
95 /* Add another type here. */
96 if (status & MR_FS_GROUPQUOTA)
99 strcat(status_buf, " / ");
100 strcat(status_buf, "Group Quotas Enabled");
103 if (status_buf[0] == '\0')
104 strcat(status_buf, "-- None --");
107 sprintf(buf, "Machine: %-20s Directory: %-15s Device: %s",
108 info[NFS_NAME], info[NFS_DIR], info[NFS_DEVICE]);
110 sprintf(buf, "Status: %s", status_buf);
112 sprintf(buf, "Quota Allocated: %-17s Size: %s",
113 info[NFS_ALLOC], info[NFS_SIZE]);
115 sprintf(buf, MOD_FORMAT, info[NFS_MODBY], info[NFS_MODTIME],
118 return info[NFS_DIR];
121 /* Function Name: AskNFSInfo.
122 * Description: This function askes the user for information about a
123 * machine and saves it into a structure.
124 * Arguments: info - a pointer the the structure to put the
126 * Returns: the arglist to make the update call with.
129 char **AskNFSInfo(char **info)
131 /* Also need name of the machine in this structure. */
133 if (GetValueFromUser("Device for this filsystem", &info[NFS_DEVICE]) ==
136 if (GetFSTypes(&info[NFS_STATUS], TRUE) == SUB_ERROR)
138 if (GetValueFromUser("Allocated Space for this filsystem:",
139 &info[NFS_ALLOC]) == SUB_ERROR)
141 if (GetValueFromUser("Size of this Filsystem:", &info[NFS_SIZE]) ==
145 FreeAndClear(&info[NFS_MODTIME], TRUE);
146 FreeAndClear(&info[NFS_MODBY], TRUE);
147 FreeAndClear(&info[NFS_MODWITH], TRUE);
153 /* Function Name: ShowNFSService
154 * Description: This function prints all exported partitions.
155 * Arguments: argc, argv - argv[1] - name of machine.
156 * Returns: DM_NORMAL.
159 int ShowNFSService(int argc, char **argv)
162 struct qelem *elem = NULL;
165 if (!ValidName(argv[1]))
168 args[0] = canonicalize_hostname(strsave(argv[1]));
169 args[1] = strsave(DEFAULT_DIR);
170 if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
173 if ((stat = do_mr_query("get_nfsphys", 2, args,
175 com_err(program_name, stat, " in ShowNFSServices.");
177 free(args[1]); /* prevents memory leaks. */
179 elem = QueueTop(elem);
180 Loop(elem, (void *) PrintNFSInfo);
186 /* Function Name: AddNFSService
187 * Description: Adds a new partition to the nfsphys relation
188 * Arguments: arc, argv - argv[1] - machine name.
189 * Returns: DM_NORMAL.
192 int AddNFSService(int argc, char **argv)
194 char **add_args, *args[10];
195 char *info[MAX_ARGS_SIZE];
198 args[0] = canonicalize_hostname(strsave(argv[1]));
199 args[1] = strsave(DEFAULT_DIR);
200 if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
203 if (!ValidName(args[0]) || !ValidName(args[1]))
206 if (!(stat = do_mr_query("get_nfsphys", 2, args, NullFunc, NULL)))
208 if (stat != MR_NO_MATCH)
210 com_err(program_name, stat, " in get_nfsphys.");
214 info[NFS_NAME] = Strsave(args[0]);
215 info[NFS_DIR] = args[1]; /* already saved. */
216 info[NFS_DEVICE] = Strsave(DEFAULT_DEVICE);
217 info[NFS_STATUS] = Strsave(DEFAULT_STATUS);
218 info[NFS_ALLOC] = Strsave(DEFAULT_ALLOC);
219 info[NFS_SIZE] = Strsave(DEFAULT_SIZE);
220 info[NFS_MODBY] = info[NFS_MODWITH] = info[NFS_MODTIME] = NULL;
221 info[NFS_END] = NULL;
223 if (!(add_args = AskNFSInfo(info)))
225 Put_message("Aborted.");
229 if ((stat = do_mr_query("add_nfsphys", CountArgs(add_args), add_args,
231 com_err(program_name, stat, " in AdsNFSService");
238 /* Function Name: RealUpdateNFSService
239 * Description: performs the actual update of the nfs service.
240 * Arguments: info - info about NFS service stored in array of strings.
241 * junk - an unused boolean.
245 static void RealUpdateNFSService(char **info, Bool junk)
250 if (!(args = AskNFSInfo(info)))
252 Put_message("Aborted.");
256 if ((stat = do_mr_query("update_nfsphys", CountArgs(args), args,
258 com_err(program_name, stat, " in RealUpdateNFSService");
261 /* Function Name: UpdateNFSService
262 * Description: Update the values for an nfsphys entry.
263 * Arguments: argc, argv - argv[1] - machine name.
264 * Returns: DM_NORMAL.
267 int UpdateNFSService(int argc, char **argv)
270 struct qelem *elem = NULL;
273 if (!ValidName(argv[1]))
276 args[0] = canonicalize_hostname(strsave(argv[1]));
277 args[1] = strsave(DEFAULT_DIR);
278 if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
281 if ((stat = do_mr_query("get_nfsphys", 2, args, StoreInfo, (char *) &elem)))
283 com_err(program_name, stat, " in UpdateNFSService.");
287 free(args[1]); /* stop memory leaks. */
289 elem = QueueTop(elem);
290 QueryLoop(elem, UpdatePrint, RealUpdateNFSService, "Update NFS Service for");
296 /* Function Name: FSPartPrint
297 * Description: print filesystem partition usage.
298 * Arguments: info - the filesystem information.
302 static void FSPartPrint(char **info)
305 sprintf(buf, "NFS Filesystem %s uses that partition.", info[FS_NAME]);
309 /* Function Name: RealDeleteNFSService
310 * Description: Actually Deletes the filesystem (some checks are made).
311 * Arguments: info - info about NFS service stored in array of strings.
312 * one_item - if TRUE then only one item on the queue, and
317 static void RealDeleteNFSService(char **info, Bool one_item)
319 char temp_buf[BUFSIZ], *args[10];
320 struct qelem *elem = NULL;
324 "Are you sure that you want to delete the %s directory on %s",
325 info[NFS_DIR], info[NFS_NAME]);
328 * Check to be sure that it is not used by any of the nfs packs.
331 if (!one_item || Confirm(temp_buf))
333 args[0] = info[NFS_NAME];
334 args[1] = info[NFS_DIR];
336 switch ((stat = do_mr_query("get_filesys_by_nfsphys", CountArgs(args),
337 args, StoreInfo, (char *)&elem)))
339 case MR_NO_MATCH: /* it is unused, delete it. */
340 if ((stat = do_mr_query("delete_nfsphys", 2, info, Scream, NULL)))
341 com_err(program_name, stat, " in DeleteNFSService");
343 Put_message("Physical Filesystem Deleted.");
345 case MR_SUCCESS: /* it is used, print filesys's that use it. */
346 elem = QueueTop(elem);
347 Put_message("The following fileystems are using this partition,");
348 Put_message("and must be removed before it can be deleted.");
350 Loop(elem, FSPartPrint);
356 com_err(program_name, stat, " while checking usage of partition");
360 Put_message("Physical filesystem not deleted.");
363 /* Function Name: DeleteNFSService
364 * Description: Delete an nfsphys entry.
365 * Arguments: argc, argv - name of the machine in argv[1].
366 * Returns: DM_NORMAL.
369 int DeleteNFSService(int argc, char **argv)
372 struct qelem *elem = NULL;
375 if (!ValidName(argv[1]))
378 args[0] = canonicalize_hostname(strsave(argv[1]));
379 args[1] = strsave(DEFAULT_DIR);
380 if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR)
383 switch ((stat = do_mr_query("get_nfsphys", 2, args,
384 StoreInfo, (char *) &elem)))
387 Put_message("This filsystem does not exist!");
392 com_err(program_name, stat, " in DeleteNFSService");
396 free(args[1]); /* stop memory leaks, in your neighborhood. */
398 QueryLoop(elem, PrintNFSInfo, RealDeleteNFSService,
399 "Delete the Physical Filesystem on Directory");