/[dynamips]/upstream/dynamips-0.2.6-RC3/utils.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /upstream/dynamips-0.2.6-RC3/utils.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

upstream/dynamips-0.2.5/utils.c revision 1 by dpavlin, Sat Oct 6 16:01:44 2007 UTC upstream/dynamips-0.2.6-RC3/utils.c revision 4 by dpavlin, Sat Oct 6 16:06:49 2007 UTC
# Line 12  Line 12 
12  #include <stdarg.h>  #include <stdarg.h>
13  #include <unistd.h>  #include <unistd.h>
14  #include <time.h>  #include <time.h>
15    #include <signal.h>
16  #include <sys/time.h>  #include <sys/time.h>
17  #include <sys/ioctl.h>  #include <sys/ioctl.h>
18  #include <sys/types.h>  #include <sys/types.h>
19    #include <sys/stat.h>
20  #include <sys/socket.h>  #include <sys/socket.h>
21  #include <arpa/inet.h>  #include <arpa/inet.h>
22  #include <netdb.h>  #include <netdb.h>
# Line 297  void *m_memalign(size_t boundary,size_t Line 299  void *m_memalign(size_t boundary,size_t
299  #ifdef __linux__  #ifdef __linux__
300     if (posix_memalign((void *)&p,boundary,size))     if (posix_memalign((void *)&p,boundary,size))
301  #else  #else
302  #ifdef __CYGWIN__  #if defined(__CYGWIN__) || defined(SUNOS)
303     if (!(p = memalign(boundary,size)))     if (!(p = memalign(boundary,size)))
304  #else  #else
305     if (!(p = malloc(size)))         if (!(p = malloc(size)))    
# Line 309  void *m_memalign(size_t boundary,size_t Line 311  void *m_memalign(size_t boundary,size_t
311     return p;     return p;
312  }  }
313    
314    /* Block specified signal for calling thread */
315    int m_signal_block(int sig)
316    {
317       sigset_t sig_mask;
318       sigemptyset(&sig_mask);
319       sigaddset(&sig_mask,sig);
320       return(pthread_sigmask(SIG_BLOCK,&sig_mask,NULL));
321    }
322    
323    /* Unblock specified signal for calling thread */
324    int m_signal_unblock(int sig)
325    {
326       sigset_t sig_mask;
327       sigemptyset(&sig_mask);
328       sigaddset(&sig_mask,sig);
329       return(pthread_sigmask(SIG_UNBLOCK,&sig_mask,NULL));
330    }
331    
332    /* Set non-blocking mode on a file descriptor */
333    int m_fd_set_non_block(int fd)
334    {
335       int flags;
336    
337       if ((flags = fcntl(fd,F_GETFL,0)) < 1)
338          return(-1);
339    
340       return(fcntl(fd,F_SETFL, flags | O_NONBLOCK));
341    }
342    
343    /* Map a memory zone from a file */
344    u_char *memzone_map_file(int fd,size_t len)
345    {
346       return(mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fd,(off_t)0));
347    }
348    
349    /* Map a memory zone from a file, with copy-on-write (COW) */
350    u_char *memzone_map_cow_file(int fd,size_t len)
351    {
352       return(mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_PRIVATE,fd,(off_t)0));
353    }
354    
355    /* Create a file to serve as a memory zone */
356    int memzone_create_file(char *filename,size_t len,u_char **ptr)
357    {
358       int fd;
359    
360       if ((fd = open(filename,O_CREAT|O_RDWR,S_IRWXU)) == -1) {
361          perror("memzone_create_file: open");
362          return(-1);
363       }
364    
365       if (ftruncate(fd,len) == -1) {
366          perror("memzone_create_file: ftruncate");
367          close(fd);
368          return(-1);
369       }
370    
371       *ptr = memzone_map_file(fd,len);
372    
373       if (!*ptr) {
374          close(fd);
375          fd = -1;
376       }
377    
378       return(fd);
379    }
380    
381    /* Open a file to serve as a COW memory zone */
382    int memzone_open_cow_file(char *filename,size_t len,u_char **ptr)
383    {
384       int fd;
385    
386       if ((fd = open(filename,O_RDWR,S_IRWXU)) == -1) {
387          perror("memzone_open_file: open");
388          return(-1);
389       }
390    
391       *ptr = memzone_map_cow_file(fd,len);
392    
393       if (!*ptr) {
394          close(fd);
395          fd = -1;
396       }
397    
398       return(fd);
399    }
400    
401    /* Open a file and map it in memory */
402    int memzone_open_file(char *filename,u_char **ptr,off_t *fsize)
403    {
404       struct stat fprop;
405       int fd;
406    
407       if ((fd = open(filename,O_RDWR,S_IRWXU)) == -1)
408          return(-1);
409    
410       if (fstat(fd,&fprop) == -1)
411          goto err_fstat;
412    
413       *fsize = fprop.st_size;
414       if (!(*ptr = memzone_map_file(fd,*fsize)))
415          goto err_mmap;
416      
417       return(fd);
418    
419     err_mmap:  
420     err_fstat:
421       close(fd);
422       return(-1);
423    }
424    
425    /* Compute NVRAM checksum */
426    m_uint16_t nvram_cksum(m_uint16_t *ptr,size_t count)
427    {
428       m_uint32_t sum = 0;
429    
430       while(count > 1) {
431          sum = sum + ntohs(*ptr);
432          ptr++;
433          count -= sizeof(m_uint16_t);
434       }
435    
436       if (count > 0)
437          sum = sum + ((ntohs(*ptr) & 0xFF) << 8);
438    
439       while(sum>>16)
440          sum = (sum & 0xffff) + (sum >> 16);
441    
442       return(~sum);
443    }

Legend:
Removed from v.1  
changed lines
  Added in v.4

  ViewVC Help
Powered by ViewVC 1.1.26