~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/wingrid/wingrid.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <grp.h>
 
34
#include <pwd.h>
 
35
#include <errno.h>
 
36
#include <fcntl.h>
 
37
#include <unistd.h>
 
38
#include <string.h>
 
39
#include <stdlib.h>
 
40
#include <pthread.h>
 
41
#include <rpc/rpc.h>
 
42
#include <sys/types.h>
 
43
#include <sys/socket.h>
 
44
#include <arpa/inet.h> 
 
45
#include <sys/statvfs.h>
 
46
#include <interix/interix.h>
 
47
#include <interix/security.h>
 
48
 
 
49
#include "sgermon.h"
 
50
#include "sge_log.h"
 
51
#include "basis_types.h"
 
52
#include "../../../utilbin/sge_passwd.h"
 
53
#include "msg_wingrid.h"
 
54
#include "config_file.h"
 
55
#include "sge_bootstrap.h"
 
56
#include "wingrid.h"
 
57
 
 
58
static bool wl_enable_windomacc = false;
 
59
 
 
60
/* ============================================================================
 
61
 * STATIC Functions
 
62
 * ==========================================================================*/
 
63
/****** wingrid/wl_copy_string_to_buffer() ***********************************
 
64
*  NAME
 
65
*     wl_copy_string_to_buffer() -- Copies one string to a position in a
 
66
*                                   buffer, sets buffer position and 
 
67
*                                   remaining size of buffer.
 
68
*
 
69
*  SYNOPSIS
 
70
*     static int wl_copy_string_to_buffer(const char *string, char *buf,
 
71
*                                          int *bufpos, int *bufremain)
 
72
*
 
73
*  FUNCTION
 
74
*     Copies a string into a buffer at given position. Checks before copying
 
75
*     if the string will fit into the buffer. After copying, sets the index
 
76
*     of the next free buffer element and the size of the remaining free
 
77
*     buffer, so a subsequent call to this function will copy the next
 
78
*     string to the next free location in the buffer.
 
79
*
 
80
*  INPUTS
 
81
*     const char *string    - The string that is to copy into the buffer.
 
82
*     char       *buf       - Pointer to beginning of the buffer.
 
83
*     int        *bufpos    - Pointer to the Index of the position in the buffer,
 
84
*                             where the string should be copied to.
 
85
*     int        *bufremain - Pointer to the number of remaining free bytes in
 
86
*                             the buffer before copying.
 
87
*
 
88
*  OUTPUTS
 
89
*     int        *bufpos    - Pointer to the Index of the next free position
 
90
*                             in the buffer.
 
91
*     int        *bufremain - Pointer to the number of remaining free bytes in
 
92
*                             the buffer after copying.
 
93
*
 
94
*  RESULTS
 
95
*     int - 0 if OK, 1 if the buffer is too small.
 
96
*
 
97
*  NOTES
 
98
*     MT-NOTE: wl_copy_string_to_buffer() is MT-safe
 
99
******************************************************************************/
 
100
static int wl_copy_string_to_buffer(const char *string, char *buf, 
 
101
                                    int *bufpos, int *bufremain)
 
102
{
 
103
   int stringlen;
 
104
 
 
105
   stringlen = strlen(string)+1; /* Include trailing '\0' */
 
106
   if(stringlen > *bufremain) {
 
107
      return 1;
 
108
   }
 
109
   memcpy(&buf[*bufpos], (unsigned char*)string, stringlen);
 
110
   *bufpos += stringlen;
 
111
   *bufremain -= stringlen;
 
112
 
 
113
   return 0;
 
114
}
 
115
 
 
116
/****** wingrid/wl_getpwuid_ex_r() *******************************************
 
117
*  NAME
 
118
*     wl_getpwuid_ex_r() -- MT safe version of extended Interix version of
 
119
*                           getpwuid()
 
120
*
 
121
*  SYNOPSIS
 
122
*     int wl_getpwuid_ex_r(uid_t uid, struct passwd *pwd, char *buffer,
 
123
*                          size_t bufsize, struct passwd **result, uint flags)
 
124
*
 
125
*  FUNCTION
 
126
*     MT safe function to retrieve passwd information for a specific UID.
 
127
*     Uses Interix' extended version of getpwuid() that allows to retrieve
 
128
*     full qualified Windows domain user names.
 
129
*     E.g.: "BOFUR+Administrator" instead of "Administrator"
 
130
*
 
131
*  INPUTS
 
132
*     uid_t           uid     - The UID of the passwd
 
133
*     struct passwd  *pwd     - Pointer of the passwd struct that is to be filled.
 
134
*     char           *buffer  - Buffer providing memory for the strings in passwd. 
 
135
*     size_t          bufsize - Size of the buffer.
 
136
*     uint            flags   - 0:           short name is retrieved
 
137
*                               PW_FULLNAME: FQDN name is retrieved
 
138
*
 
139
*  OUTPUTS
 
140
*     struct passwd **result  - Points to pwd after function call, 
 
141
*                               see getpwuid_r().
 
142
*
 
143
*  RESULTS
 
144
*     int - 0 if OK, 1 if the buffer is too small.
 
145
*
 
146
*  NOTES
 
147
*     MT-NOTE: wl_getpwuid_ex_r() is not MT-safe
 
148
*              It is safe it getpwuid_ex() is not called from any other place.
 
149
*
 
150
*  SEE ALSO
 
151
*     getpwuid_r(), getpwuid_ex()
 
152
******************************************************************************/
 
153
int wl_getpwuid_ex_r(uid_t uid, struct passwd *pwd, char *buffer,
 
154
                    size_t bufsize, struct passwd **result, uint flags)
 
155
{
 
156
   static pthread_mutex_t getpwuid_ex_mutex = PTHREAD_MUTEX_INITIALIZER;
 
157
   int ret;
 
158
   int bufpos;
 
159
   int bufremain;
 
160
   struct passwd *pwd_ex;
 
161
 
 
162
   pthread_mutex_lock(&getpwuid_ex_mutex);
 
163
 
 
164
   pwd_ex = getpwuid_ex(uid, flags);
 
165
   pwd->pw_uid    = pwd_ex->pw_uid;
 
166
   pwd->pw_gid    = pwd_ex->pw_gid;
 
167
   pwd->pw_change = pwd_ex->pw_change;
 
168
   pwd->pw_expire = pwd_ex->pw_expire;
 
169
 
 
170
   bufpos    = 0;
 
171
   bufremain = bufsize;
 
172
 
 
173
   pwd->pw_name = &buffer[bufpos];
 
174
   ret = wl_copy_string_to_buffer(pwd_ex->pw_name, buffer, &bufpos, &bufremain);
 
175
   if(ret==0) {
 
176
      pwd->pw_dir = &buffer[bufpos];
 
177
      ret = wl_copy_string_to_buffer(pwd_ex->pw_dir, buffer, &bufpos, &bufremain);
 
178
   }
 
179
   if(ret==0) {
 
180
      pwd->pw_shell = &buffer[bufpos];
 
181
      ret = wl_copy_string_to_buffer(pwd_ex->pw_shell, buffer, &bufpos, &bufremain);
 
182
   }
 
183
   if(ret==0) {
 
184
      pwd->pw_passwd = &buffer[bufpos];
 
185
      ret = wl_copy_string_to_buffer(pwd_ex->pw_passwd, buffer, &bufpos, &bufremain);
 
186
   }
 
187
   if(ret==0) {
 
188
      pwd->pw_gecos = &buffer[bufpos];
 
189
      ret = wl_copy_string_to_buffer(pwd_ex->pw_gecos, buffer, &bufpos, &bufremain);
 
190
   }
 
191
   result = &pwd;
 
192
 
 
193
   pthread_mutex_unlock(&getpwuid_ex_mutex);
 
194
   return ret;
 
195
}
 
196
 
 
197
/****** wingrid/wl_handle_ls_results() **************************************
 
198
*  NAME
 
199
*     wl_handle_ls_results() -- Parse result of external load sensor
 
200
*
 
201
*  SYNOPSIS
 
202
*     bool wl_handle_ls_results(const char *name, const char *value,
 
203
*                               const char *host, char *error_string)
 
204
*
 
205
*  FUNCTION
 
206
*     Parses the result of an externel loadsensor. Returns true if the 
 
207
*     loadsensors reported load, returns false if the loadsensor
 
208
*     reported an error.
 
209
*
 
210
*  RESULT
 
211
*     bool - true if load is OK, false if load is an error report.
 
212
*
 
213
*  NOTES
 
214
*     MT-NOTE: wl_handle_ls_results() is MT safe
 
215
******************************************************************************/
 
216
/* returns true if name/value/host is a loadvalue */
 
217
/* error_string should have a size of at least 4 * MAX_STRING_SIZE */
 
218
bool wl_handle_ls_results(const char *name, 
 
219
                          const char *value, 
 
220
                          const char *host,
 
221
                          char *error_string) 
 
222
{
 
223
   bool ret = false; 
 
224
   
 
225
   if (strcmp(host, "_sge_pseudo_host") == 0) {
 
226
      if (strncmp(name, "error", 5) == 0) {
 
227
         if (error_string != NULL) {
 
228
            sprintf(error_string, "loadsensor report: %s", value);
 
229
         }
 
230
      }
 
231
   } else {
 
232
      ret = true;
 
233
   }
 
234
   return ret;
 
235
}
 
236
 
 
237
/* ============================================================================
 
238
 * SUPERUSER
 
239
 * ==========================================================================*/
 
240
/****** wingrid/wl_get_superuser_id() ***************************************
 
241
*  NAME
 
242
*     wl_get_superuser_id() -- return UID of local Administrator
 
243
*
 
244
*  SYNOPSIS
 
245
*     uid_t wl_get_superuser_id()
 
246
*
 
247
*  FUNCTION
 
248
*     Return UID of local Administrator.
 
249
*     It is under Windows, unlike Unix, not always 0.
 
250
*     Only the local Administrator is comparable to root, the Domain
 
251
*     Administrator has only limited abilities.
 
252
*
 
253
*  RESULT
 
254
*     uid_t - the UID of the local Administrator.
 
255
*
 
256
*  NOTES
 
257
*     MT-NOTE: wl_get_superuser_id() is MT safe
 
258
******************************************************************************/
 
259
uid_t wl_get_superuser_id()
 
260
{
 
261
   return 197108;
 
262
}
 
263
 
 
264
gid_t wl_get_superuser_gid()
 
265
{
 
266
   return 197121;
 
267
}
 
268
 
 
269
/****** wingrid/wl_get_superuser_name() **************************************
 
270
*  NAME
 
271
*     wl_get_superuser_name() -- return name of local Administrator
 
272
*
 
273
*  SYNOPSIS
 
274
*     int wl_get_superuser_name(char *buf, int bufsize)
 
275
*
 
276
*  FUNCTION
 
277
*     Return name of local Administrator.
 
278
*     The name depends on the language of Windows and could be set manually
 
279
*     by the Admnistrator.
 
280
*
 
281
*  INPUTS
 
282
*     bufsize - sife of buffer that is to receive superuser name.
 
283
*
 
284
*  OUTPUTS
 
285
*     char *buf - Pointer to a buffer that is to receive superuser name.
 
286
*
 
287
*  RESULT
 
288
*     int - 0: OK
 
289
*           1: buffer to small for superuser name
 
290
*
 
291
*  NOTES
 
292
*     MT-NOTE: wl_get_superuser_name() is MT safe
 
293
******************************************************************************/
 
294
int wl_get_superuser_name(char *buf, int bufsize)
 
295
{
 
296
   struct passwd  pwd, *pwdresult;
 
297
   char           buffer[4096];
 
298
   int            ret;
 
299
   uid_t          uid = wl_get_superuser_id();
 
300
 
 
301
   ret = wl_getpwuid_ex_r(uid, &pwd, buffer, sizeof(buffer), &pwdresult, 0);
 
302
   if(ret == 0) {
 
303
      strlcpy(buf, pwd.pw_name, bufsize);
 
304
   }
 
305
   return ret;
 
306
}
 
307
 
 
308
/****** wingrid/wl_is_user_id_superuser()*************************************
 
309
*  NAME
 
310
*     wl_is_user_id_superuser() -- check if UID is superuser
 
311
*                                  (= local Administrator)
 
312
*
 
313
*  SYNOPSIS
 
314
*     bool wl_is_user_id_superuser(int uid)
 
315
*
 
316
*  FUNCTION
 
317
*     Check if UID is superuser (=local Administrator)
 
318
*
 
319
*  INPUTS
 
320
*     uid - the UID that is to be checked.
 
321
*
 
322
*  RESULT
 
323
*     bool - true: UID is superuser, false: UID is not superuser
 
324
*
 
325
*  NOTES
 
326
*     MT-NOTE: wl_is_user_id_superuser() is MT safe
 
327
******************************************************************************/
 
328
bool wl_is_user_id_superuser(int uid)
 
329
{
 
330
   /* Only the local Administrator is the 'real' superuser! */
 
331
   return (uid==wl_get_superuser_id());
 
332
}
 
333
 
 
334
/* ============================================================================
 
335
 * SETUSER
 
336
 * ==========================================================================*/
 
337
/****** wingrid/wl_setuser() **********************************************
 
338
*  NAME
 
339
*     wl_setuser() --  change effective and real uid and gid of process
 
340
*
 
341
*  SYNOPSIS
 
342
*     int wl_setuser(int uid, int gid)
 
343
*
 
344
*  FUNCTION
 
345
*     Change effective and real uid and gid of process
 
346
*
 
347
*  INPUTS
 
348
*     int uid - UID that the process should be changed to.
 
349
*     int gid - GID that the process should be changed to.
 
350
*
 
351
*  RESULTS
 
352
*     int - 0 if uid and gid changed correctly, else errno is set and:
 
353
*           1 can't read passwd information
 
354
*           2
 
355
*
 
356
*  NOTES
 
357
*     MT-NOTE: wl_setuser() is MT safe
 
358
******************************************************************************/
 
359
int wl_setuser(int uid, int gid, const char *pass, char* err_str)
 
360
{
 
361
   char   buf[16000];
 
362
   int    ret=0;
 
363
   int    try=0;
 
364
   struct passwd pwd, *ppwd=NULL;
 
365
 
 
366
   /* Do this only for domain users */
 
367
   if(uid>999999) { 
 
368
      /*
 
369
       * Get username of UID
 
370
       * (Use not MT safe getpwuid_ex() for full qualified name)
 
371
       */
 
372
      ret = wl_getpwuid_ex_r(uid, &pwd, buf, sizeof(buf), &ppwd, 0); 
 
373
      if(ret != 0) {
 
374
         sprintf(err_str, MSG_WIN_CANT_GET_PASSWD_INFO, uid);
 
375
      } else {
 
376
         /* 
 
377
          * Read password from file
 
378
          */
 
379
         if(pwd.pw_name==NULL || 
 
380
            (pwd.pw_name!=NULL && pwd.pw_name[0]=='\0')) {
 
381
            sprintf(err_str, MSG_WIN_CANT_GET_PASSWD_INFO, uid);
 
382
            ret = 1;
 
383
         } else {
 
384
            ret = setuser(pwd.pw_name, pass, SU_CHECK);
 
385
            if(ret != 0) {
 
386
               sprintf(err_str, MSG_WIN_CHECK_SETUSER_FAILED, pwd.pw_name,
 
387
                       strerror(errno)?strerror(errno):"<NULL>", errno);
 
388
            } else {
 
389
               do {
 
390
                  try++;
 
391
                  ret = setuser(pwd.pw_name, pass, SU_COMPLETE);
 
392
 
 
393
                  if(ret != 0) {
 
394
                     sleep(1);
 
395
                  } 
 
396
               } while(ret != 0 && try < 5);
 
397
 
 
398
               if(ret != 0) {
 
399
                  sprintf(err_str, MSG_WIN_SETUSER_FAILED, pwd.pw_name,
 
400
                          strerror(errno)?strerror(errno):"<NULL>", errno);
 
401
               }
 
402
            }
 
403
         }
 
404
      }
 
405
   }
 
406
   else
 
407
   {
 
408
      ret = setgid(gid);
 
409
      if(ret != 0) {
 
410
         sprintf(err_str, MSG_WIN_CANT_SETGID_IN_SETUSER, gid, 
 
411
                 strerror(errno)?strerror(errno):"<NULL>", errno);
 
412
      } else {
 
413
         ret = setuid(uid);
 
414
         if(ret != 0) {
 
415
            sprintf(err_str, MSG_WIN_CANT_SETUID_IN_SETUSER, uid,
 
416
                    strerror(errno)?strerror(errno):"<NULL>", errno);
 
417
         }
 
418
      }
 
419
   }
 
420
   return ret;
 
421
}
 
422
 
 
423
bool wl_use_sgepasswd()
 
424
{
 
425
   return wl_enable_windomacc;
 
426
}
 
427
 
 
428
void wl_set_use_sgepasswd(bool use_it)
 
429
{
 
430
   wl_enable_windomacc = use_it;
 
431
}
 
432
 
 
433
/* ============================================================================
 
434
 * User Name modifications
 
435
 * ==========================================================================*/
 
436
/****** wingrid/wl_strip_hostname() ******************************************
 
437
*  NAME
 
438
*     wl_strip_hostname() -- strip hostname from full qualified username  
 
439
*
 
440
*  SYNOPSIS
 
441
*     char *wl_strip_hostname(char *user_name)
 
442
*
 
443
*  FUNCTION
 
444
*     Strips the hostname from the full qualified username, i.e.
 
445
*     returns "username" for "HOST+username".
 
446
*
 
447
*  INPUTS
 
448
*     char *user_name - Pointer to a dynamically allocated buffer that
 
449
*                       contains the full qualified username. This buffer
 
450
*                       gets freed in this function.
 
451
*
 
452
*  RESULTS
 
453
*     char * - Pointer to a dynamically allocated buffer that contains
 
454
*              the stripped user name. This buffer must be freed after usage.
 
455
*
 
456
*  NOTES
 
457
*     MT-NOTE: wl_strip_hostname() is MT-safe.
 
458
*
 
459
*  SEE ALSO
 
460
******************************************************************************/
 
461
char *wl_strip_hostname(char *user_name)
 
462
{
 
463
   char *token;
 
464
   char *ret   = strdup(user_name);
 
465
   char *lasts = NULL;
 
466
 
 
467
   token = strtok_r(user_name, "+", &lasts);
 
468
   if (token != NULL) {
 
469
      token = strtok_r(NULL, " ", &lasts);
 
470
      if (token != NULL) {
 
471
         ret = strdup(token);
 
472
      }
 
473
   }
 
474
   free(user_name);
 
475
   return ret;
 
476
}
 
477
 
 
478
/****** wingrid/wl_build_fq_local_name() *************************************
 
479
*  NAME
 
480
*     wl_build_fq_local_name() -- build full qualified local user name
 
481
*
 
482
*  SYNOPSIS
 
483
*     int wl_build_fq_local_name(const char *user, char *fq_name)
 
484
*
 
485
*  FUNCTION
 
486
*     Builds a full qualified Interix user name from the given short 
 
487
*     user name. A full qualified username is of the form
 
488
*     DOMAIN+USER, where DOMAIN is in this case the name of the local host.
 
489
*
 
490
*  INPUTS
 
491
*     const char *user - short name of the user
 
492
*
 
493
*  OUTPUTS
 
494
*     char *fq_name - buffer that is to receive the full qualified name.
 
495
*                     It's size must be 1024.
 
496
*
 
497
*  RESULTS
 
498
*     int - 0 if full qualified name was successfully build
 
499
*           1 if the user name was not provided in short format
 
500
*
 
501
*  NOTES
 
502
*     MT-NOTE: wl_build_fq_local_name() is MT-safe.
 
503
*
 
504
*  SEE ALSO
 
505
******************************************************************************/
 
506
int wl_build_fq_local_name(const char *user, char *fq_name)
 
507
{
 
508
   char hostname[128];
 
509
   int  ret = 1;
 
510
 
 
511
   /*
 
512
    * do only if user name is not already full qualified
 
513
    */
 
514
   if(user != NULL && fq_name != NULL && strstr(user, "+")==NULL) {
 
515
      gethostname(hostname, 128);
 
516
      snprintf(fq_name, 1023, "%s+%s", hostname, user);
 
517
      ret = 0;
 
518
   }
 
519
   return ret;
 
520
}
 
521
 
 
522
/* ============================================================================
 
523
 * REPLACEMENTS of buggy libc functions
 
524
 * ==========================================================================*/
 
525
/* ----------------------------------------------------------------------------
 
526
 * STAT
 
527
 * --------------------------------------------------------------------------*/
 
528
/****** wingrid/wl_stat() / wingrid/wl_statvfs()***************************
 
529
*  NAME
 
530
*     wl_stat() / wl_statvfs() - obtain info about files
 
531
*
 
532
*  SYNOPSIS
 
533
*     int wl_stat(const char *path, struct stat *buf)
 
534
*     int wl_statvfs(const char *path, struct statvfs *buf)
 
535
*
 
536
*  FUNCTION
 
537
*     Obtain information about the file pointed to by path.
 
538
*     This functions are replacements for the libc functions of Interix
 
539
*     that don't work on network drives.
 
540
*     Limitation of this functions: The process must have the permissions
 
541
*     to open the file for reading.
 
542
*
 
543
*  INPUTS
 
544
*     const char     *path - path of the file to obtain info about.
 
545
*
 
546
*  OUTPUTS
 
547
*     struct stat    *buf  - Information bout the file.
 
548
*     struct statvfs *buf  - 
 
549
*
 
550
*  RESULT
 
551
*     int - 0 on successful completion, otherwise -1 and errno is set.
 
552
*
 
553
*  NOTES
 
554
*     MT-NOTE: wl_stat() and wl_statvfs are MT safe.
 
555
******************************************************************************/
 
556
int wl_stat(const char *path, struct stat *buf)
 
557
{
 
558
   /* stat() doesn't work on NFS drives, have to use fstat() */
 
559
   int fd;
 
560
   int ret=-1;
 
561
 
 
562
   if((fd = open(path, O_RDONLY))!=-1) {
 
563
      ret = fstat(fd, buf);
 
564
      close(fd);
 
565
   }
 
566
   return ret;
 
567
}
 
568
 
 
569
int wl_statvfs(const char *path, struct statvfs *buf)
 
570
{
 
571
   /* statvfs() doesn't work on NFS drives, have to use fstatvfs() */
 
572
   int fd;
 
573
   int ret=-1;
 
574
            
 
575
   if((fd = open(path, O_RDONLY))!=-1) {
 
576
      ret = fstatvfs(fd, buf);
 
577
      close(fd);
 
578
   }
 
579
   return ret;
 
580
}
 
581
 
 
582
/* ----------------------------------------------------------------------------
 
583
 * XDR
 
584
 * --------------------------------------------------------------------------*/
 
585
/****** wingrid/wl_xdrmem_create() ******************************************
 
586
*  NAME
 
587
*     wl_xdrmem_create() -- Initialize XDR stream
 
588
*
 
589
*  SYNOPSIS
 
590
*     void wl_xdrmem_create(XDR *xdrs, const caddr_t addr,
 
591
*                     const uint_t size, const enum xdr_op op)
 
592
*
 
593
*  FUNCTION
 
594
*     Initializes XDR stream for usage.
 
595
*
 
596
*  INPUTS
 
597
*     XDR               *xdrs - XDR stream
 
598
*     const caddr_t     addr  - Buffer
 
599
*     const uint_t      size  - Size of buffer
 
600
*     const enum xdr_op op    - Direction of XDR stream (encode or decode)
 
601
*
 
602
*  OUTPUTS
 
603
*     XDR *xdrs - Initialized XDR stream
 
604
*
 
605
*  NOTES
 
606
*     MT-NOTE: wl_xdrmem_create() is MT safe
 
607
******************************************************************************/
 
608
void wl_xdrmem_create(XDR *xdrs, const caddr_t addr,
 
609
                      const uint_t size, const enum xdr_op op)
 
610
{
 
611
   if(xdrs!=NULL) {
 
612
      xdrs->x_op      = op;
 
613
      xdrs->x_ops     = NULL;
 
614
      xdrs->x_public  = addr;
 
615
      xdrs->x_private = NULL;
 
616
      xdrs->x_base    = 0;
 
617
      xdrs->x_handy   = 0;
 
618
   }
 
619
}
 
620
 
 
621
/****** wingrid/wl_xdr_destroy() ********************************************
 
622
*  NAME
 
623
*     wl_xdr_destroy() -- Destroy XDR stream
 
624
*
 
625
*  SYNOPSIS
 
626
*     void wl_xdr_destroy(XDR *xdrs)
 
627
*
 
628
*  FUNCTION
 
629
*     Destroys XDR stream. Doesn't free any buffers!
 
630
*
 
631
*  INPUTS
 
632
*     XDR               *xdrs - XDR stream
 
633
*
 
634
*  NOTES
 
635
*     MT-NOTE: wl_xdr_destroy() is MT safe
 
636
******************************************************************************/
 
637
void wl_xdr_destroy(XDR *xdrs)
 
638
{
 
639
}
 
640
 
 
641
/****** wingrid/wl_xdr_double() *********************************************
 
642
*  NAME
 
643
*     wl_xdr_double() -- Convert a double
 
644
*
 
645
*  SYNOPSIS
 
646
*     bool_t wl_xdr_double(XDR *xdrs, double *dp)
 
647
*
 
648
*  FUNCTION
 
649
*     Encodes or decodes a variable of type double.
 
650
*
 
651
*  INPUTS
 
652
*     XDR    *xdrs - If decoding: Stream that contains the encoded value
 
653
*     double *dp   - If encoding: Pointer to the variable that is to encode
 
654
*
 
655
*  OUTPUTS
 
656
*     XDR    *xdrs - If encoding: Stream that contains the encoded value
 
657
*     double *dp   - If decoding: Pointer to the variable that receives value
 
658
*
 
659
*  NOTES
 
660
*     MT-NOTE: wl_xdr_double() is MT safe
 
661
******************************************************************************/
 
662
bool_t wl_xdr_double(XDR *xdrs, double *dp)
 
663
{
 
664
   int i;
 
665
 
 
666
   if(xdrs==NULL || dp==NULL) {
 
667
      return FALSE;
 
668
   }
 
669
 
 
670
   if(xdrs->x_op==XDR_ENCODE) 
 
671
   {
 
672
      for(i=0; i<8; i++) {
 
673
         ((char*)xdrs->x_public)[i+xdrs->x_handy] = ((char*)dp)[7-i];
 
674
      }
 
675
      xdrs->x_handy += 8;
 
676
   }
 
677
   else if(xdrs->x_op==XDR_DECODE)
 
678
   {
 
679
      for(i=0; i<8; i++) {
 
680
         ((char*)dp)[7-i] = ((char*)xdrs->x_public)[i+xdrs->x_handy];
 
681
      }
 
682
      xdrs->x_handy += 8;
 
683
   }
 
684
   return TRUE;  
 
685
}
 
686
 
 
687
/****** wingrid/wl_xdr_getpos() *********************************************
 
688
*  NAME
 
689
*     wl_xdr_getpos() -- Get current position in stream
 
690
*
 
691
*  SYNOPSIS
 
692
*     uint_t wl_xdr_getpos(const XDR *xdrs)
 
693
*
 
694
*  FUNCTION
 
695
*     Returns position in XDR stream.
 
696
*
 
697
*  INPUTS
 
698
*     const XDR *xdrs - Stream on which position is to be returned.
 
699
*
 
700
*  RESULTS
 
701
*     uint_t - Current position on stream.
 
702
*
 
703
*  NOTES
 
704
*     MT-NOTE: wl_xdr_getpos() is MT safe
 
705
******************************************************************************/
 
706
uint_t wl_xdr_getpos(const XDR *xdrs)
 
707
{
 
708
   return xdrs->x_handy;
 
709
}