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