Home | History | Annotate | Download | only in libnewipc
      1 /*
      2  * Copyright (c) 2016 Xiao Yang <yangx.jy (at) cn.fujitsu.com>
      3  *
      4  * This program is free software;  you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation; either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     12  * the GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program.
     16  */
     17 
     18 /*
     19  * DESCRIPTION
     20  * common routines for the IPC system call tests.
     21  */
     22 
     23 #include <errno.h>
     24 #include <unistd.h>
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <sys/types.h>
     28 #include <sys/ipc.h>
     29 
     30 #define	TST_NO_DEFAULT_MAIN
     31 
     32 #include "tst_test.h"
     33 #include "libnewipc.h"
     34 
     35 #define BUFSIZE 1024
     36 
     37 key_t getipckey(const char *file, const int lineno)
     38 {
     39 	char buf[BUFSIZE];
     40 	key_t key;
     41 	int id;
     42 	static int count;
     43 
     44 	SAFE_GETCWD(buf, BUFSIZE);
     45 
     46 	id = count % 26 + (int) 'a';
     47 	count++;
     48 
     49 	key = ftok(buf, id);
     50 	if (key == -1) {
     51 		tst_brk(TBROK | TERRNO,
     52 			"ftok() failed at %s:%d", file, lineno);
     53 	}
     54 
     55 	return key;
     56 }
     57 
     58 int get_used_queues(const char *file, const int lineno)
     59 {
     60 	FILE *fp;
     61 	int used_queues = -1;
     62 	char buf[BUFSIZE];
     63 
     64 	fp = fopen("/proc/sysvipc/msg", "r");
     65 	if (fp == NULL) {
     66 		tst_brk(TBROK | TERRNO,
     67 			"fopen() failed at %s:%d", file, lineno);
     68 	}
     69 
     70 	while (fgets(buf, BUFSIZE, fp) != NULL)
     71 		used_queues++;
     72 
     73 	fclose(fp);
     74 
     75 	if (used_queues < 0) {
     76 		tst_brk(TBROK, "can't read /proc/sysvipc/msg to get "
     77 			"used message queues at %s:%d", file, lineno);
     78 	}
     79 
     80 	return used_queues;
     81 }
     82