/alps/ipecamera

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to pcitool/sysinfo.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-04-27 00:28:57 UTC
  • Revision ID: csa@suren.me-20150427002857-82fk6r3e8rfgy4wr
First stand-alone ipecamera implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
#include <sys/types.h>
4
 
#include <sys/stat.h>
5
 
#include <fcntl.h>
6
 
#include <unistd.h>
7
 
#include <string.h>
8
 
#include <strings.h>
9
 
 
10
 
#define MEMINFO_FILE "/proc/meminfo"
11
 
#define MTAB_FILE "/etc/mtab"
12
 
 
13
 
#define BAD_OPEN_MESSAGE                                        \
14
 
"Error: /proc must be mounted\n"                                \
15
 
"  To mount /proc at boot you need an /etc/fstab line like:\n"  \
16
 
"      /proc   /proc   proc    defaults\n"                      \
17
 
"  In the meantime, run \"mount /proc /proc -t proc\"\n"
18
 
 
19
 
/* This macro opens filename only if necessary and seeks to 0 so
20
 
 * that successive calls to the functions are more efficient.
21
 
 * It also reads the current contents of the file into the global buf.
22
 
 */
23
 
#define FILE_TO_BUF(filename) do{                               \
24
 
    static int fd, local_n;                                     \
25
 
    if ((fd = open(filename, O_RDONLY)) == -1) {                \
26
 
        fputs(BAD_OPEN_MESSAGE, stderr);                        \
27
 
        fflush(NULL);                                           \
28
 
        _exit(102);                                             \
29
 
    }                                                           \
30
 
    lseek(fd, 0L, SEEK_SET);                                    \
31
 
    if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) {        \
32
 
        perror(filename);                                       \
33
 
        fflush(NULL);                                           \
34
 
        _exit(103);                                             \
35
 
    }                                                           \
36
 
    buf[local_n] = '\0';                                        \
37
 
    close(fd);                                                  \
38
 
}while(0)
39
 
 
40
 
 
41
 
typedef struct mem_table_struct {
42
 
  const char *name;     /* memory type name */
43
 
  unsigned long *slot; /* slot in return struct */
44
 
} mem_table_struct;
45
 
 
46
 
static int compare_mem_table_structs(const void *a, const void *b){
47
 
  return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
48
 
}
49
 
 
50
 
size_t get_free_memory(void){
51
 
  char buf[4096];
52
 
  unsigned long kb_main_buffers, kb_main_cached, kb_main_free;
53
 
  char namebuf[16]; /* big enough to hold any row name */
54
 
  mem_table_struct findme = { namebuf, NULL};
55
 
  mem_table_struct *found;
56
 
  char *head;
57
 
  char *tail;
58
 
 
59
 
  const mem_table_struct mem_table[] = {
60
 
    {"Buffers",      &kb_main_buffers}, // important
61
 
    {"Cached",       &kb_main_cached},  // important
62
 
    {"MemFree",      &kb_main_free},    // important
63
 
  };
64
 
  const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
65
 
 
66
 
  FILE_TO_BUF(MEMINFO_FILE);
67
 
 
68
 
  head = buf;
69
 
  for(;;){
70
 
    tail = strchr(head, ':');
71
 
    if(!tail) break;
72
 
    *tail = '\0';
73
 
    if(strlen(head) >= sizeof(namebuf)){
74
 
      head = tail+1;
75
 
      goto nextline;
76
 
    }
77
 
    strcpy(namebuf,head);
78
 
    found = bsearch(&findme, mem_table, mem_table_count,
79
 
        sizeof(mem_table_struct), compare_mem_table_structs
80
 
    );
81
 
    head = tail+1;
82
 
    if(!found) goto nextline;
83
 
    *(found->slot) = strtoul(head,&tail,10);
84
 
nextline:
85
 
    tail = strchr(head, '\n');
86
 
    if(!tail) break;
87
 
    head = tail+1;
88
 
  }
89
 
  
90
 
  return (kb_main_buffers + kb_main_cached + kb_main_free) * 1024;
91
 
}
92
 
 
93
 
 
94
 
int get_file_fs(const char *fname, size_t size, char *fs) {
95
 
  int err = 0;
96
 
  char buf[4096];
97
 
  char *fn;
98
 
 
99
 
  char *head;
100
 
  char *tail;
101
 
 
102
 
  size_t len, max = 0;
103
 
  struct stat st;
104
 
  
105
 
  if ((!fname)||(!fs)||(size < 3)) return -1;
106
 
  
107
 
  if (*fname == '/') {
108
 
    fn = (char*)fname;
109
 
  } else {
110
 
    if (!getcwd(buf, 4095)) return -1;
111
 
    fn = malloc(strlen(fname) + strlen(buf) + 2);
112
 
    if (!fn) return -1;
113
 
    sprintf(fn, "%s/%s", buf, fname);
114
 
  }
115
 
  
116
 
  if (!stat(fn, &st)) {
117
 
    if (S_ISBLK(st.st_mode)) {
118
 
        strcpy(fs, "raw");
119
 
        goto clean;
120
 
    }
121
 
  }
122
 
  
123
 
  FILE_TO_BUF(MTAB_FILE);
124
 
 
125
 
  head = buf;
126
 
  for(;;){
127
 
    head = strchr(head, ' ');
128
 
    if(!head) break;
129
 
 
130
 
    head += 1;
131
 
    tail = strchr(head, ' ');
132
 
    if(!tail) break;
133
 
    
134
 
    *tail = '\0';
135
 
 
136
 
    len = strlen(head);
137
 
    if((len <= max)||(strncmp(head, fn, len))) {
138
 
      head = tail+1;
139
 
      goto nextline;
140
 
    }
141
 
    
142
 
    head = tail + 1;
143
 
    tail = strchr(head, ' ');
144
 
    if(!tail) break;
145
 
 
146
 
    *tail = '\0';
147
 
 
148
 
    if (!strncasecmp(head,"root",4)) {
149
 
        head = tail+1;
150
 
        goto nextline;
151
 
    }
152
 
    
153
 
    max = len;
154
 
 
155
 
    if (strlen(head) >= size) err = -1;
156
 
    else {
157
 
        err = 0;
158
 
        strcpy(fs, head);
159
 
    }
160
 
    
161
 
    head = tail+1;
162
 
nextline:
163
 
    tail = strchr(head, '\n');
164
 
    if(!tail) break;
165
 
    head = tail+1;
166
 
  }
167
 
 
168
 
clean:  
169
 
  if (fn != fname) free(fn);
170
 
 
171
 
  return err;
172
 
}