1 | /* $Id: nfs.c,v 1.26 1999-04-30 17:41:08 danw Exp $ |
---|
2 | * |
---|
3 | * This is the file nfs.c for the Moira Client, which allows users |
---|
4 | * to quickly and easily maintain most parts of the Moira database. |
---|
5 | * It Contains: All functions for manipulating NFS Physical directories. |
---|
6 | * |
---|
7 | * Created: 5/6/88 |
---|
8 | * By: Chris D. Peterson |
---|
9 | * |
---|
10 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
---|
11 | * For copying and distribution information, please see the file |
---|
12 | * <mit-copyright.h>. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <mit-copyright.h> |
---|
16 | #include <moira.h> |
---|
17 | #include <moira_site.h> |
---|
18 | #include "defs.h" |
---|
19 | #include "f_defs.h" |
---|
20 | #include "globals.h" |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/clients/moira/nfs.c,v 1.26 1999-04-30 17:41:08 danw Exp $"); |
---|
27 | |
---|
28 | char **AskNFSInfo(char **info); |
---|
29 | |
---|
30 | #define TYPE_NFS "NFS" |
---|
31 | |
---|
32 | #define DEFAULT_DIR "/u1/lockers" |
---|
33 | #define DEFAULT_DEVICE "/dev/ra1c" |
---|
34 | #define DEFAULT_STATUS DEFAULT_YES /* active. */ |
---|
35 | #define DEFAULT_ALLOC "0" |
---|
36 | #define DEFAULT_SIZE "0" |
---|
37 | |
---|
38 | /* Function Name: UpdatePrint |
---|
39 | * Description: store a useful string for updates to print. |
---|
40 | * Arguments: info - info about NFS service stored in array of strings. |
---|
41 | * Returns: useful string. |
---|
42 | */ |
---|
43 | |
---|
44 | static char *UpdatePrint(char **info) |
---|
45 | { |
---|
46 | char temp_buf[BUFSIZ]; |
---|
47 | sprintf(temp_buf, "Machine %s Directory %s", info[NFS_NAME], info[NFS_DIR]); |
---|
48 | return strdup(temp_buf); /* Small memory leak here, but no good way |
---|
49 | to avoid it that I see. */ |
---|
50 | } |
---|
51 | |
---|
52 | /* Function Name: PrintNFSInfo |
---|
53 | * Description: Prints NFS Physical service information. |
---|
54 | * Arguments: info - the information. |
---|
55 | * Returns: directory of this nfs server, for DeleteNFSService(). |
---|
56 | */ |
---|
57 | |
---|
58 | static char *PrintNFSInfo(char **info) |
---|
59 | { |
---|
60 | char buf[BUFSIZ], status_buf[BUFSIZ]; |
---|
61 | int status = atoi(info[NFS_STATUS]); |
---|
62 | Bool is_one = FALSE; |
---|
63 | |
---|
64 | status_buf[0] = '\0'; /* clear string. */ |
---|
65 | |
---|
66 | if (status & MR_FS_STUDENT) |
---|
67 | { |
---|
68 | strcat(status_buf, "Student"); |
---|
69 | is_one = TRUE; |
---|
70 | } |
---|
71 | if (status & MR_FS_FACULTY) |
---|
72 | { |
---|
73 | if (is_one) |
---|
74 | strcat(status_buf, " and "); |
---|
75 | strcat(status_buf, "Faculty"); |
---|
76 | is_one = TRUE; |
---|
77 | } |
---|
78 | if (status & MR_FS_STAFF) |
---|
79 | { |
---|
80 | if (is_one) |
---|
81 | strcat(status_buf, " and "); |
---|
82 | strcat(status_buf, "Staff"); |
---|
83 | is_one = TRUE; |
---|
84 | } |
---|
85 | if (status & MR_FS_MISC) |
---|
86 | { |
---|
87 | if (is_one) |
---|
88 | strcat(status_buf, " and "); |
---|
89 | strcat(status_buf, "Miscellaneous"); |
---|
90 | } |
---|
91 | /* Add another type here. */ |
---|
92 | if (status & MR_FS_GROUPQUOTA) |
---|
93 | { |
---|
94 | if (is_one) |
---|
95 | strcat(status_buf, " / "); |
---|
96 | strcat(status_buf, "Group Quotas Enabled"); |
---|
97 | } |
---|
98 | |
---|
99 | if (status_buf[0] == '\0') |
---|
100 | strcat(status_buf, "-- None --"); |
---|
101 | |
---|
102 | Put_message(""); |
---|
103 | sprintf(buf, "Machine: %-20s Directory: %-15s Device: %s", |
---|
104 | info[NFS_NAME], info[NFS_DIR], info[NFS_DEVICE]); |
---|
105 | Put_message(buf); |
---|
106 | sprintf(buf, "Status: %s", status_buf); |
---|
107 | Put_message(buf); |
---|
108 | sprintf(buf, "Quota Allocated: %-17s Size: %s", |
---|
109 | info[NFS_ALLOC], info[NFS_SIZE]); |
---|
110 | Put_message(buf); |
---|
111 | sprintf(buf, MOD_FORMAT, info[NFS_MODBY], info[NFS_MODTIME], |
---|
112 | info[NFS_MODWITH]); |
---|
113 | Put_message(buf); |
---|
114 | return info[NFS_DIR]; |
---|
115 | } |
---|
116 | |
---|
117 | /* Function Name: AskNFSInfo. |
---|
118 | * Description: This function askes the user for information about a |
---|
119 | * machine and saves it into a structure. |
---|
120 | * Arguments: info - a pointer the the structure to put the |
---|
121 | * info into. |
---|
122 | * Returns: the arglist to make the update call with. |
---|
123 | */ |
---|
124 | |
---|
125 | char **AskNFSInfo(char **info) |
---|
126 | { |
---|
127 | /* Also need name of the machine in this structure. */ |
---|
128 | |
---|
129 | if (GetValueFromUser("Device for this filsystem", &info[NFS_DEVICE]) == |
---|
130 | SUB_ERROR) |
---|
131 | return NULL; |
---|
132 | if (GetFSTypes(&info[NFS_STATUS], TRUE) == SUB_ERROR) |
---|
133 | return NULL; |
---|
134 | if (GetValueFromUser("Allocated Space for this filsystem:", |
---|
135 | &info[NFS_ALLOC]) == SUB_ERROR) |
---|
136 | return NULL; |
---|
137 | if (GetValueFromUser("Size of this Filsystem:", &info[NFS_SIZE]) == |
---|
138 | SUB_ERROR) |
---|
139 | return NULL; |
---|
140 | |
---|
141 | FreeAndClear(&info[NFS_MODTIME], TRUE); |
---|
142 | FreeAndClear(&info[NFS_MODBY], TRUE); |
---|
143 | FreeAndClear(&info[NFS_MODWITH], TRUE); |
---|
144 | |
---|
145 | return info; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | /* Function Name: ShowNFSService |
---|
150 | * Description: This function prints all exported partitions. |
---|
151 | * Arguments: argc, argv - argv[1] - name of machine. |
---|
152 | * Returns: DM_NORMAL. |
---|
153 | */ |
---|
154 | |
---|
155 | int ShowNFSService(int argc, char **argv) |
---|
156 | { |
---|
157 | int stat; |
---|
158 | struct mqelem *elem = NULL; |
---|
159 | char *args[10]; |
---|
160 | |
---|
161 | if (!ValidName(argv[1])) |
---|
162 | return DM_NORMAL; |
---|
163 | |
---|
164 | args[0] = canonicalize_hostname(strdup(argv[1])); |
---|
165 | args[1] = strdup(DEFAULT_DIR); |
---|
166 | if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR) |
---|
167 | return DM_NORMAL; |
---|
168 | |
---|
169 | if ((stat = do_mr_query("get_nfsphys", 2, args, StoreInfo, &elem))) |
---|
170 | com_err(program_name, stat, " in ShowNFSServices."); |
---|
171 | free(args[0]); |
---|
172 | free(args[1]); /* prevents memory leaks. */ |
---|
173 | |
---|
174 | elem = QueueTop(elem); |
---|
175 | Loop(elem, (void (*)(char **)) PrintNFSInfo); |
---|
176 | |
---|
177 | FreeQueue(elem); |
---|
178 | return DM_NORMAL; |
---|
179 | } |
---|
180 | |
---|
181 | /* Function Name: AddNFSService |
---|
182 | * Description: Adds a new partition to the nfsphys relation |
---|
183 | * Arguments: arc, argv - argv[1] - machine name. |
---|
184 | * Returns: DM_NORMAL. |
---|
185 | */ |
---|
186 | |
---|
187 | int AddNFSService(int argc, char **argv) |
---|
188 | { |
---|
189 | char **add_args, *args[10]; |
---|
190 | char *info[MAX_ARGS_SIZE]; |
---|
191 | int stat; |
---|
192 | |
---|
193 | args[0] = canonicalize_hostname(strdup(argv[1])); |
---|
194 | args[1] = strdup(DEFAULT_DIR); |
---|
195 | if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR) |
---|
196 | return DM_NORMAL; |
---|
197 | |
---|
198 | if (!ValidName(args[0]) || !ValidName(args[1])) |
---|
199 | return DM_NORMAL; |
---|
200 | |
---|
201 | if (!(stat = do_mr_query("get_nfsphys", 2, args, NULL, NULL))) |
---|
202 | stat = MR_EXISTS; |
---|
203 | if (stat != MR_NO_MATCH) |
---|
204 | { |
---|
205 | com_err(program_name, stat, " in get_nfsphys."); |
---|
206 | return DM_NORMAL; |
---|
207 | } |
---|
208 | |
---|
209 | info[NFS_NAME] = strdup(args[0]); |
---|
210 | info[NFS_DIR] = args[1]; /* already saved. */ |
---|
211 | info[NFS_DEVICE] = strdup(DEFAULT_DEVICE); |
---|
212 | info[NFS_STATUS] = strdup(DEFAULT_STATUS); |
---|
213 | info[NFS_ALLOC] = strdup(DEFAULT_ALLOC); |
---|
214 | info[NFS_SIZE] = strdup(DEFAULT_SIZE); |
---|
215 | info[NFS_MODBY] = info[NFS_MODWITH] = info[NFS_MODTIME] = NULL; |
---|
216 | info[NFS_END] = NULL; |
---|
217 | |
---|
218 | if (!(add_args = AskNFSInfo(info))) |
---|
219 | { |
---|
220 | Put_message("Aborted."); |
---|
221 | return DM_NORMAL; |
---|
222 | } |
---|
223 | |
---|
224 | if ((stat = do_mr_query("add_nfsphys", CountArgs(add_args), add_args, |
---|
225 | NULL, NULL))) |
---|
226 | com_err(program_name, stat, " in AdsNFSService"); |
---|
227 | |
---|
228 | FreeInfo(info); |
---|
229 | free(args[0]); |
---|
230 | return DM_NORMAL; |
---|
231 | } |
---|
232 | |
---|
233 | /* Function Name: RealUpdateNFSService |
---|
234 | * Description: performs the actual update of the nfs service. |
---|
235 | * Arguments: info - info about NFS service stored in array of strings. |
---|
236 | * junk - an unused boolean. |
---|
237 | * Returns: none. |
---|
238 | */ |
---|
239 | |
---|
240 | static void RealUpdateNFSService(char **info, Bool junk) |
---|
241 | { |
---|
242 | char **args; |
---|
243 | int stat; |
---|
244 | |
---|
245 | if (!(args = AskNFSInfo(info))) |
---|
246 | { |
---|
247 | Put_message("Aborted."); |
---|
248 | return; |
---|
249 | } |
---|
250 | |
---|
251 | if ((stat = do_mr_query("update_nfsphys", CountArgs(args), args, |
---|
252 | NULL, NULL))) |
---|
253 | com_err(program_name, stat, " in RealUpdateNFSService"); |
---|
254 | } |
---|
255 | |
---|
256 | /* Function Name: UpdateNFSService |
---|
257 | * Description: Update the values for an nfsphys entry. |
---|
258 | * Arguments: argc, argv - argv[1] - machine name. |
---|
259 | * Returns: DM_NORMAL. |
---|
260 | */ |
---|
261 | |
---|
262 | int UpdateNFSService(int argc, char **argv) |
---|
263 | { |
---|
264 | int stat; |
---|
265 | struct mqelem *elem = NULL; |
---|
266 | char *args[10]; |
---|
267 | |
---|
268 | if (!ValidName(argv[1])) |
---|
269 | return DM_NORMAL; |
---|
270 | |
---|
271 | args[0] = canonicalize_hostname(strdup(argv[1])); |
---|
272 | args[1] = strdup(DEFAULT_DIR); |
---|
273 | if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR) |
---|
274 | return DM_NORMAL; |
---|
275 | |
---|
276 | if ((stat = do_mr_query("get_nfsphys", 2, args, StoreInfo, &elem))) |
---|
277 | { |
---|
278 | com_err(program_name, stat, " in UpdateNFSService."); |
---|
279 | return DM_NORMAL; |
---|
280 | } |
---|
281 | free(args[0]); |
---|
282 | free(args[1]); /* stop memory leaks. */ |
---|
283 | |
---|
284 | elem = QueueTop(elem); |
---|
285 | QueryLoop(elem, UpdatePrint, RealUpdateNFSService, "Update NFS Service for"); |
---|
286 | |
---|
287 | FreeQueue(elem); |
---|
288 | return DM_NORMAL; |
---|
289 | } |
---|
290 | |
---|
291 | /* Function Name: FSPartPrint |
---|
292 | * Description: print filesystem partition usage. |
---|
293 | * Arguments: info - the filesystem information. |
---|
294 | * Returns: none. |
---|
295 | */ |
---|
296 | |
---|
297 | static void FSPartPrint(char **info) |
---|
298 | { |
---|
299 | char buf[BUFSIZ]; |
---|
300 | sprintf(buf, "NFS Filesystem %s uses that partition.", info[FS_NAME]); |
---|
301 | Put_message(buf); |
---|
302 | } |
---|
303 | |
---|
304 | /* Function Name: RealDeleteNFSService |
---|
305 | * Description: Actually Deletes the filesystem (some checks are made). |
---|
306 | * Arguments: info - info about NFS service stored in array of strings. |
---|
307 | * one_item - if TRUE then only one item on the queue, and |
---|
308 | * we should confirm. |
---|
309 | * Returns: none. |
---|
310 | */ |
---|
311 | |
---|
312 | static void RealDeleteNFSService(char **info, Bool one_item) |
---|
313 | { |
---|
314 | char temp_buf[BUFSIZ], *args[10]; |
---|
315 | struct mqelem *elem = NULL; |
---|
316 | int stat; |
---|
317 | |
---|
318 | sprintf(temp_buf, |
---|
319 | "Are you sure that you want to delete the %s directory on %s", |
---|
320 | info[NFS_DIR], info[NFS_NAME]); |
---|
321 | |
---|
322 | /* |
---|
323 | * Check to be sure that it is not used by any of the nfs packs. |
---|
324 | */ |
---|
325 | |
---|
326 | if (!one_item || Confirm(temp_buf)) |
---|
327 | { |
---|
328 | args[0] = info[NFS_NAME]; |
---|
329 | args[1] = info[NFS_DIR]; |
---|
330 | args[2] = NULL; |
---|
331 | switch ((stat = do_mr_query("get_filesys_by_nfsphys", CountArgs(args), |
---|
332 | args, StoreInfo, &elem))) |
---|
333 | { |
---|
334 | case MR_NO_MATCH: /* it is unused, delete it. */ |
---|
335 | if ((stat = do_mr_query("delete_nfsphys", 2, info, NULL, NULL))) |
---|
336 | com_err(program_name, stat, " in DeleteNFSService"); |
---|
337 | else |
---|
338 | Put_message("Physical Filesystem Deleted."); |
---|
339 | break; |
---|
340 | case MR_SUCCESS: /* it is used, print filesys's that use it. */ |
---|
341 | elem = QueueTop(elem); |
---|
342 | Put_message("The following fileystems are using this partition,"); |
---|
343 | Put_message("and must be removed before it can be deleted."); |
---|
344 | Put_message(""); |
---|
345 | Loop(elem, FSPartPrint); |
---|
346 | |
---|
347 | FreeQueue(elem); |
---|
348 | Put_message(""); |
---|
349 | break; |
---|
350 | default: |
---|
351 | com_err(program_name, stat, " while checking usage of partition"); |
---|
352 | } |
---|
353 | } |
---|
354 | else |
---|
355 | Put_message("Physical filesystem not deleted."); |
---|
356 | } |
---|
357 | |
---|
358 | /* Function Name: DeleteNFSService |
---|
359 | * Description: Delete an nfsphys entry. |
---|
360 | * Arguments: argc, argv - name of the machine in argv[1]. |
---|
361 | * Returns: DM_NORMAL. |
---|
362 | */ |
---|
363 | |
---|
364 | int DeleteNFSService(int argc, char **argv) |
---|
365 | { |
---|
366 | int stat; |
---|
367 | struct mqelem *elem = NULL; |
---|
368 | char *args[10]; |
---|
369 | |
---|
370 | if (!ValidName(argv[1])) |
---|
371 | return DM_NORMAL; |
---|
372 | |
---|
373 | args[0] = canonicalize_hostname(strdup(argv[1])); |
---|
374 | args[1] = strdup(DEFAULT_DIR); |
---|
375 | if (GetValueFromUser("Directory:", &args[1]) == SUB_ERROR) |
---|
376 | return DM_NORMAL; |
---|
377 | |
---|
378 | switch ((stat = do_mr_query("get_nfsphys", 2, args, StoreInfo, &elem))) |
---|
379 | { |
---|
380 | case MR_NO_MATCH: |
---|
381 | Put_message("This filsystem does not exist!"); |
---|
382 | return DM_NORMAL; |
---|
383 | case MR_SUCCESS: |
---|
384 | break; |
---|
385 | default: |
---|
386 | com_err(program_name, stat, " in DeleteNFSService"); |
---|
387 | return DM_NORMAL; |
---|
388 | } |
---|
389 | free(args[0]); |
---|
390 | free(args[1]); /* stop memory leaks, in your neighborhood. */ |
---|
391 | |
---|
392 | QueryLoop(elem, PrintNFSInfo, RealDeleteNFSService, |
---|
393 | "Delete the Physical Filesystem on Directory"); |
---|
394 | |
---|
395 | FreeQueue(elem); |
---|
396 | return DM_NORMAL; |
---|
397 | } |
---|