]> andersk Git - moira.git/blob - clients/moira/nfs.c
b7d252dfa416b05ad2a69662d18e2b37bd5d4b75
[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 SMS Client, which allows a nieve
6  *      user to quickly and easily maintain most parts of the SMS 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 <sms.h>
25 #include <sms_app.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 & SMS_FS_STUDENT) {
77         strcat(status_buf, "Student");
78         is_one = TRUE;
79     }
80     if (status & SMS_FS_FACULTY) {
81         if (is_one)
82             strcat(status_buf, " and ");
83         strcat(status_buf, "Faculty");
84         is_one = TRUE;
85     }
86     if (status & SMS_FS_STAFF) {
87         if (is_one)
88             strcat(status_buf, " and ");
89         strcat(status_buf, "Staff");
90         is_one = TRUE;
91     }
92     if (status & SMS_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] = CanonicalizeHostname(argv[1]);    
178     if ( (args[1] = GetDirName()) == NULL)
179         return(DM_NORMAL);
180     
181     if ( (stat = do_sms_query("get_nfsphys", 2, args,
182                               StoreInfo, (char *)  &elem)) != SMS_SUCCESS)
183         com_err(program_name, stat, " in ShowNFSServices.");
184     free(args[1]);              /* prevents memory leaks. */
185
186     elem = QueueTop(elem);
187     Loop(elem, (void *) PrintNFSInfo);
188
189     FreeQueue(elem);
190     return (DM_NORMAL);
191 }
192
193 /*      Function Name: AddNFSService
194  *      Description: Adds a new partition to the nfsphys relation
195  *      Arguments: arc, argv - argv[1] - machine name.
196  *      Returns: DM_NORMAL.
197  */
198
199 /* ARGSUSED */
200 int
201 AddNFSService(argc, argv)
202 char **argv;
203 int argc;
204 {
205     char **add_args, *args[10];
206     char *info[MAX_ARGS_SIZE];
207     int stat;
208
209     args[0] = CanonicalizeHostname(argv[1]);
210     if ( (args[1] = GetDirName()) == NULL)
211         return(DM_NORMAL);
212     
213     if (!ValidName(args[0]) || !ValidName(args[1]))
214         return(DM_NORMAL);
215     
216     if ( (stat = do_sms_query("get_nfsphys", 2, args,
217                               NullFunc, (char *) NULL)) == SMS_SUCCESS)
218         Put_message("This service already exists.");
219     if (stat != SMS_NO_MATCH) 
220         com_err(program_name, stat, " in get_nfsphys.");
221     
222     info[NFS_NAME]   = Strsave(args[0]);
223     info[NFS_DIR]    = args[1]; /* already saved. */
224     info[NFS_DEVICE] = Strsave(DEFAULT_DEVICE);
225     info[NFS_STATUS] = Strsave(DEFAULT_STATUS);
226     info[NFS_ALLOC]  = Strsave(DEFAULT_ALLOC);
227     info[NFS_SIZE]   = Strsave(DEFAULT_SIZE);
228     info[NFS_MODBY] = info[NFS_MODWITH] = info[NFS_MODTIME] = NULL;
229     info[NFS_END] = NULL;       
230
231     add_args = AskNFSInfo(info);
232     
233     if ((stat = do_sms_query("add_nfsphys", CountArgs(add_args), add_args,
234                              Scream, (char *) NULL)) != 0) 
235         com_err(program_name, stat, " in AdsNFSService");
236     
237     FreeInfo(info);
238     return (DM_NORMAL);
239 }
240
241 /*      Function Name: RealUpdateNFSService
242  *      Description: performs the actual update of the nfs service.
243  *      Arguments: info - info about NFS service stored in array of strings.
244  *                 junk - an unused boolean.
245  *      Returns: none.
246  */
247
248 /* ARGSUSED */
249 static void
250 RealUpdateNFSService(info, junk)
251 char ** info;
252 Bool junk;
253 {
254     char ** args;
255     register int stat;
256     
257     args = AskNFSInfo(info);
258     if ((stat = do_sms_query("update_nfsphys", CountArgs(args), args,
259                              Scream, (char *)NULL)) != SMS_SUCCESS) 
260         com_err(program_name, stat, (char *) NULL);
261 }
262
263 /*      Function Name: UpdateNFSService
264  *      Description: Update the values for an nfsphys entry.
265  *      Arguments: argc, argv - argv[1] - machine name.
266  *      Returns: DM_NORMAL.
267  */
268
269 /* ARGSUSED. */
270 int
271 UpdateNFSService(argc, argv)
272 char **argv;
273 int argc;
274 {
275     register int stat;
276     struct qelem *elem = NULL;
277     char * args[10];
278
279     if (!ValidName(argv[1]))
280         return(DM_NORMAL);
281
282     args[0] = CanonicalizeHostname(argv[1]);
283     if ( (args[1] = GetDirName()) == NULL)
284         return(DM_NORMAL);
285
286     if ( (stat = do_sms_query("get_nfsphys", 2, args,
287                               StoreInfo, (char *) &elem)) != SMS_SUCCESS) {
288         com_err(program_name, stat, " in UpdateNFSService.");
289         return (DM_NORMAL);
290     }
291     free(args[1]);              /* stop memory leaks. */
292
293     elem = QueueTop(elem);
294     QueryLoop(elem, UpdatePrint, RealUpdateNFSService, 
295               "Update NFS Service for");
296
297     FreeQueue(elem);
298     return (DM_NORMAL);
299 }
300
301 /*      Function Name: FSPartPrint
302  *      Description: print filesystem partition usage.
303  *      Arguments: info - the filesystem information.
304  *      Returns: none.
305  */
306
307 static void
308 FSPartPrint(info)
309 char ** info;
310 {
311     char buf[BUFSIZ];
312     sprintf(buf, "NFS Filesystem %s uses that partition.", info[FS_NAME]);
313     Put_message(buf);
314 }
315
316 /*      Function Name: RealDeleteNFSService
317  *      Description: Actually Deletes the filesystem (some checks are made).
318  *      Arguments: info - info about NFS service stored in array of strings.
319  *                 one_item - if TRUE then only one item on the queue, and
320  *                            we should confirm.
321  *      Returns: none.
322  */
323
324 static void
325 RealDeleteNFSService(info, one_item)
326 char ** info;
327 Bool one_item;
328 {
329     char temp_buf[BUFSIZ], *args[10];
330     struct qelem *elem= NULL;
331     register int stat;
332     
333     sprintf(temp_buf,
334             "Are you sure that you want to delete the %s directory on %s",
335             info[NFS_DIR],info[NFS_NAME]);
336
337 /* 
338  * Check to be sure that it is not used by any of the nfs packs.
339  */
340
341     if (!one_item || Confirm(temp_buf)) {
342         args[0] = info[NFS_NAME];
343         args[1] = info[NFS_DIR];
344         args[2] = NULL;
345         switch(stat = do_sms_query("get_filesys_by_nfsphys", CountArgs(args), 
346                                    args, StoreInfo, (char *)&elem)) {
347         case SMS_NO_MATCH:      /* it is unused, delete it. */
348             if ( (stat = do_sms_query("delete_nfsphys", 2, info, Scream, 
349                                       (char *) NULL )) != SMS_SUCCESS)
350                 com_err(program_name, stat, " in DeleteNFSService");
351             else
352                 Put_message("Physical Filesystem Deleted.");
353             break;
354         case SMS_SUCCESS:       /* it is used, print filesys's that use it. */
355             elem = QueueTop(elem);
356             Put_message("The following fileystems are using this partition,");
357             Put_message("and must be removed before it can be deleted.");
358             Put_message("");
359             Loop(elem, FSPartPrint);
360
361             FreeQueue(elem);
362             Put_message("");
363             break;
364         default:
365             com_err(program_name, stat, " while checking usage of partition");
366         }
367     }
368     else
369         Put_message("Physical filesystem not deleted.");
370 }
371
372 /*      Function Name: DeleteNFSService
373  *      Description: Delete an nfsphys entry.
374  *      Arguments: argc, argv - name of the machine in argv[1].
375  *      Returns: DM_NORMAL.
376  */
377
378 /* ARGSUSED */
379 int
380 DeleteNFSService(argc, argv)
381 int argc;
382 char **argv;
383 {
384     register int stat;
385     struct qelem *elem = NULL;
386     char * args[10];
387
388     if (!ValidName(argv[1]))
389         return(DM_NORMAL);
390
391     args[0] = CanonicalizeHostname(argv[1]);
392     if ( (args[1] = GetDirName()) == NULL)
393         return(DM_NORMAL);
394
395     switch(stat = do_sms_query("get_nfsphys", 2, args, 
396                                StoreInfo, (char *) &elem)) {
397     case SMS_NO_MATCH:
398         Put_message("This filsystem does not exist!");
399         return(DM_NORMAL);
400     case SMS_SUCCESS:
401         break;
402     default:
403         com_err(program_name, stat, " in DeleteNFSService");
404         return(DM_NORMAL);
405     }
406     free(args[1]);              /* stop memory leaks, in your neighborhood. */
407
408     QueryLoop(elem, PrintNFSInfo, RealDeleteNFSService,
409               "Delete the Physical Filesystem on Directory");
410
411     FreeQueue(elem);
412     return(DM_NORMAL);
413 }    
This page took 0.173404 seconds and 3 git commands to generate.