1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| #define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "request.h"
#include <pwd.h>
#define TUBE_REQUEST "tube_entree_48624821"
#define BASIC_LENGTH 50
#define UID 1
#define GID 2
#define PID 3
#define FUN_SUCCESS 0
#define FUN_FAILURE -1
struct request{
int requesttype;
int id;
char name[BASIC_LENGTH];
void *shm;
};
struct pidresponse{
char name[BASIC_LENGTH];
char state[BASIC_LENGTH];
};
struct uidresponse{
uid_t uid;
char name[BASIC_LENGTH];
gid_t gid;
char dir[BASIC_LENGTH];
char shell[BASIC_LENGTH];
};
/*
* Retourne une requete allouée utilisable directement par l'appel ask
* Attend un uid valide, renvoi NULL sinon
*/
request *uInfoWithUid(uid_t u){
request *r;
if ((r = malloc(sizeof(*r))) == NULL) {
perror("request malloc");
return NULL;
}
r -> requesttype = UID;
r -> id = (int) u;
return r;
}
/*
* Retourne une requete allouée utilisable directement par l'appel ask
* Attend un nom valide, renvoi NULL sinon
*/
request *uInfoWithName(char *name) {
request *r;
if ((r = malloc(sizeof(*r))) == NULL) {
perror("request malloc");
return NULL;
}
r -> requesttype = UID;
r -> id = -1;
strcpy(r -> name, name);
return r;
}
/*
* Retourne une requete allouée utilisable directement par l'appel ask
* Attend un pid valide, renvoi NULL sinon
*/
request *pInfoWithPid(pid_t p) {
request *r;
if ((r = malloc(sizeof(*r))) == NULL) {
perror("request malloc");
return NULL;
}
r -> requesttype = PID;
r -> id = (int) p;
return r;
}
pidresponse *makeShmP(char *shm) {
int mem = shm_open(shm, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (mem == -1) {
perror("shm_open");
return NULL;
}
if (shm_unlink(shm) == -1) {
perror("shm_unlink");
return NULL;
}
if (ftruncate(mem, sizeof(pidresponse)) == -1) {
perror("ftruncate");
return NULL;
}
pidresponse *p = mmap(NULL, sizeof(*p), PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);
if (p == MAP_FAILED) {
perror("nmap");
return NULL;
}
return p;
}
uidresponse *makeShmU(char *shm) {
int mem = shm_open(shm, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (mem == -1) {
perror("shm_open");
return NULL;
}
if (shm_unlink(shm) == -1) {
perror("shm_unlink");
return NULL;
}
if (ftruncate(mem, sizeof(uidresponse)) == -1) {
perror("ftruncate");
return NULL;
}
uidresponse *u = mmap(NULL, sizeof(*u), PROT_READ | PROT_WRITE, MAP_SHARED, mem, 0);
if (u == MAP_FAILED) {
perror("nmap");
return NULL;
}
return u;
}
int askServerInfoU(request *req, uidresponse *shm) {
int treq;
if ((treq = open(TUBE_REQUEST, O_WRONLY )) == -1) {
perror("open");
return FUN_FAILURE;
}
req -> shm = shm;
if (write(treq, req, sizeof(*req)) == -1) {
perror("write");
return FUN_FAILURE;
}
return FUN_SUCCESS;
}
int askServerInfoP(request *req, pidresponse *shm) {
int treq;
if ((treq = open(TUBE_REQUEST, O_WRONLY )) == -1) {
perror("open");
return FUN_FAILURE;
}
req -> shm = shm;
if (write(treq, req, sizeof(*req)) == -1) {
perror("write");
return FUN_FAILURE;
}
return FUN_SUCCESS;
}
char *getPName(pidresponse *p) {
return p -> name;
} |
Partager