1 #include "lib.h"
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <errno.h>
8
9 static int
note(int pid,char * msg,char * fmt)10 note(int pid, char *msg, char *fmt)
11 {
12 int f;
13 char pname[50];
14
15 sprintf(pname, fmt, pid);
16 f = open(pname, O_WRONLY);
17 if(f < 0){
18 errno = ESRCH;
19 return -1;
20 }
21 if(msg != 0 && write(f, msg, strlen(msg)) < 0){
22 close(f);
23 errno = EPERM;
24 return -1;
25 }
26 close(f);
27 return 0;
28 }
29
30 int
kill(pid_t pid,int sig)31 kill(pid_t pid, int sig)
32 {
33 char *msg;
34 int sid, r, mpid;
35
36 if(sig == 0)
37 msg = 0;
38 else {
39 msg = _sigstring(sig);
40 if(msg == 0) {
41 errno = EINVAL;
42 return -1;
43 }
44 }
45
46 if(pid < 0) {
47 sid = getpgrp();
48 mpid = getpid();
49 if(setpgid(mpid, -pid) == 0) {
50 r = note(mpid, msg, "/proc/%d/notepg");
51 setpgid(mpid, sid);
52 } else {
53 r = -1;
54 }
55 } else if(pid == 0)
56 r = note(getpid(), msg, "/proc/%d/notepg");
57 else
58 r = note(pid, msg, "/proc/%d/note");
59 return r;
60 }
61