@@ -284,98 +284,59 @@ uint64_t uv_get_total_memory(void) {
284284
285285
286286uv_err_t uv_resident_set_memory (size_t * rss ) {
287- FILE * f ;
288- int itmp ;
289- char ctmp ;
290- unsigned int utmp ;
291- size_t page_size = getpagesize () ;
292- char * cbuf ;
293- int foundExeEnd ;
294- char buf [ PATH_MAX + 1 ];
295-
296- f = fopen ( "/proc/self/stat" , "r" );
297- if (! f ) return uv__new_sys_error ( errno );
298-
299- /* PID */
300- if ( fscanf ( f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
301- /* Exec file */
302- cbuf = buf ;
303- foundExeEnd = 0 ;
304- if ( fscanf ( f , "%c" , cbuf ++ ) == 0 ) goto error ;
305- while ( 1 ) {
306- if (fscanf ( f , "%c" , cbuf ) == 0 ) goto error ;
307- if ( * cbuf == ')' ) {
308- foundExeEnd = 1 ;
309- } else if ( foundExeEnd && * cbuf == ' ' ) {
310- * cbuf = 0 ;
311- break ;
312- }
287+ char buf [ 1024 ] ;
288+ const char * s ;
289+ ssize_t n ;
290+ long val ;
291+ int fd ;
292+ int i ;
293+
294+ do
295+ fd = open ( "/proc/self/stat" , O_RDONLY );
296+ while ( fd == -1 && errno == EINTR );
297+
298+ if ( fd == -1 )
299+ return uv__new_sys_error ( errno );
300+
301+ do
302+ n = read ( fd , buf , sizeof ( buf ) - 1 ) ;
303+ while ( n == -1 && errno == EINTR ) ;
304+
305+ SAVE_ERRNO ( close ( fd ));
306+ if (n == -1 )
307+ return uv__new_sys_error ( errno );
308+ buf [ n ] = '\0' ;
309+
310+ s = strchr ( buf , ' ' ) ;
311+ if ( s == NULL )
312+ goto err ;
313313
314- cbuf ++ ;
314+ s += 1 ;
315+ if (* s != '(' )
316+ goto err ;
317+
318+ s = strchr (s , ')' );
319+ if (s == NULL )
320+ goto err ;
321+
322+ for (i = 1 ; i <= 22 ; i ++ ) {
323+ s = strchr (s + 1 , ' ' );
324+ if (s == NULL )
325+ goto err ;
315326 }
316- /* State */
317- if (fscanf (f , "%c " , & ctmp ) == 0 ) goto error ; /* coverity[secure_coding] */
318- /* Parent process */
319- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
320- /* Process group */
321- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
322- /* Session id */
323- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
324- /* TTY */
325- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
326- /* TTY owner process group */
327- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
328- /* Flags */
329- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
330- /* Minor faults (no memory page) */
331- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
332- /* Minor faults, children */
333- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
334- /* Major faults (memory page faults) */
335- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
336- /* Major faults, children */
337- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
338- /* utime */
339- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
340- /* stime */
341- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
342- /* utime, children */
343- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
344- /* stime, children */
345- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
346- /* jiffies remaining in current time slice */
347- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
348- /* 'nice' value */
349- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
350- /* jiffies until next timeout */
351- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
352- /* jiffies until next SIGALRM */
353- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
354- /* start time (jiffies since system boot) */
355- if (fscanf (f , "%d " , & itmp ) == 0 ) goto error ; /* coverity[secure_coding] */
356-
357- /* Virtual memory size */
358- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
359-
360- /* Resident set size */
361- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
362- * rss = (size_t ) utmp * page_size ;
363-
364- /* rlim */
365- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
366- /* Start of text */
367- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
368- /* End of text */
369- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
370- /* Start of stack */
371- if (fscanf (f , "%u " , & utmp ) == 0 ) goto error ; /* coverity[secure_coding] */
372-
373- fclose (f );
327+
328+ errno = 0 ;
329+ val = strtol (s , NULL , 10 );
330+ if (errno != 0 )
331+ goto err ;
332+ if (val < 0 )
333+ goto err ;
334+
335+ * rss = val * getpagesize ();
374336 return uv_ok_ ;
375337
376- error :
377- fclose (f );
378- return uv__new_sys_error (errno );
338+ err :
339+ return uv__new_artificial_error (UV_EINVAL );
379340}
380341
381342
0 commit comments