annotate ie-docker.c @ 1:3e0e4fd65313

set may use command.
author taiki
date Mon, 14 Jul 2014 16:25:26 -1000
parents ca114837b2f4
children e802f3c8af1a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
1
ca114837b2f4 add docker file
taiki
parents:
diff changeset
2
ca114837b2f4 add docker file
taiki
parents:
diff changeset
3 #include <stdlib.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
4 #include <unistd.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
5
ca114837b2f4 add docker file
taiki
parents:
diff changeset
6 #include <stdio.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
7 #include <sys/types.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
8 #include <unistd.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
9 #include <signal.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
10 #include <string.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
11
ca114837b2f4 add docker file
taiki
parents:
diff changeset
12 #include <sys/types.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
13 #include <regex.h>
ca114837b2f4 add docker file
taiki
parents:
diff changeset
14
ca114837b2f4 add docker file
taiki
parents:
diff changeset
15 /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
16
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
17 #define command "/usr/bin/docker"
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
18 #define ps_command "/usr/bin/docker ps -a"
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
19 #define run_command "run"
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
20 #define build_command "build"
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
21 #define attach_command "attach"
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
22 #define dettach_command "dettach"
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
23 #define pull_command "pull" /* download docker image command */
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
24 #define images_command "images" /* list images command */
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
25 #define commit_command "commit" /* make image command */
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
26 #define rm_command "rm" /* remove container command */
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
27 #define rmi_command "rmi" /* remove image command */
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
28 #define start_command "start"
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
29 #define stop_command "stop"
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
30
ca114837b2f4 add docker file
taiki
parents:
diff changeset
31 static char bad_name[] = "Bad process name. Try students/e11/e115711/01 or teachers/kono/02\n";
ca114837b2f4 add docker file
taiki
parents:
diff changeset
32
ca114837b2f4 add docker file
taiki
parents:
diff changeset
33 #define PSNAME_MAX (512)
ca114837b2f4 add docker file
taiki
parents:
diff changeset
34
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
35 typedef struct pslist {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
36 char name[PSNAME_MAX];
ca114837b2f4 add docker file
taiki
parents:
diff changeset
37 struct pslist *next;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
38 } PSLIST, *PSLISTPTR;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
39
ca114837b2f4 add docker file
taiki
parents:
diff changeset
40 #define NEW(type) ((type*)malloc(sizeof(type)))
ca114837b2f4 add docker file
taiki
parents:
diff changeset
41
ca114837b2f4 add docker file
taiki
parents:
diff changeset
42 /* Define global variables */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
43
ca114837b2f4 add docker file
taiki
parents:
diff changeset
44 PSLISTPTR
ca114837b2f4 add docker file
taiki
parents:
diff changeset
45 get_pslist(regex_t *list_pattern)
ca114837b2f4 add docker file
taiki
parents:
diff changeset
46 {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
47 PSLISTPTR list = NEW(PSLIST);
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
48 PSLISTPTR p = list;
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
49 p->name[0] = 0;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
50 p->next = 0;
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
51 FILE *fp = popen(ps_command,"r");
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
52 while(fgets(p->name,PSNAME_MAX,fp)!=NULL) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
53 if (regexec(list_pattern, p->name, (size_t) 0, NULL, 0)) continue;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
54 p->next = NEW(PSLIST);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
55 p = p->next;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
56 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
57 p->name[0] = 0;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
58 pclose(fp);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
59 return list;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
60 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
61
ca114837b2f4 add docker file
taiki
parents:
diff changeset
62 void
ca114837b2f4 add docker file
taiki
parents:
diff changeset
63 print_pslist(PSLISTPTR list)
ca114837b2f4 add docker file
taiki
parents:
diff changeset
64 {
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
65 for(;list && list->name[0]; list = list->next) {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
66 fprintf(stdout, " %s\n",list->name);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
67 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
68 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
69
ca114837b2f4 add docker file
taiki
parents:
diff changeset
70 int
ca114837b2f4 add docker file
taiki
parents:
diff changeset
71 check_pslist_name(PSLISTPTR list, char *arg)
ca114837b2f4 add docker file
taiki
parents:
diff changeset
72 {
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
73 for(;list && list->name[0]; list = list->next) {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
74 if (strstr(list->name,arg)!=0) return 1;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
75 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
76 return 0;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
77 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
78
ca114837b2f4 add docker file
taiki
parents:
diff changeset
79 int
ca114837b2f4 add docker file
taiki
parents:
diff changeset
80 check_name(const char *p)
ca114837b2f4 add docker file
taiki
parents:
diff changeset
81 {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
82 if (!p) return 1;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
83 for(;*p;p++) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
84 char c = *p;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
85 if (c<=' ') return 1;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
86 if (('a'<=c && c<='z') ||
ca114837b2f4 add docker file
taiki
parents:
diff changeset
87 ('0'<=c && c<='9') ||
ca114837b2f4 add docker file
taiki
parents:
diff changeset
88 ('/'==c ) ||
ca114837b2f4 add docker file
taiki
parents:
diff changeset
89 ('-'==c )) continue;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
90 return 1;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
91 printf("%c", c);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
92 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
93 return 0;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
94 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
95
ca114837b2f4 add docker file
taiki
parents:
diff changeset
96 void
ca114837b2f4 add docker file
taiki
parents:
diff changeset
97 usage()
ca114837b2f4 add docker file
taiki
parents:
diff changeset
98 {
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
99 printf("Usage:\n\trun: run process\n\tbuild: build docker process from Dockerfile\n\tattach: atach process\n\tdettach: \n\tpull: \n\timages: \n\tcommit:\n");
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
100 printf(" ps-name should be students/e11/e115711/01 or teachers/kono/02\n");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
101 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
102
ca114837b2f4 add docker file
taiki
parents:
diff changeset
103 /* main(int argc, char **argv) - main process loop */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
104
ca114837b2f4 add docker file
taiki
parents:
diff changeset
105 int main(int argc, char **argv)
ca114837b2f4 add docker file
taiki
parents:
diff changeset
106 {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
107 int gid;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
108 int uid;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
109
ca114837b2f4 add docker file
taiki
parents:
diff changeset
110 /* Set euid and egid to actual user */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
111
ca114837b2f4 add docker file
taiki
parents:
diff changeset
112 char *name = getlogin();
ca114837b2f4 add docker file
taiki
parents:
diff changeset
113 uid = getuid();
ca114837b2f4 add docker file
taiki
parents:
diff changeset
114 gid = getgid();
ca114837b2f4 add docker file
taiki
parents:
diff changeset
115 printf("uid %d gid %d name %s\n", uid,gid,name);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
116 setegid(getgid());
ca114837b2f4 add docker file
taiki
parents:
diff changeset
117 seteuid(getuid());
ca114837b2f4 add docker file
taiki
parents:
diff changeset
118
ca114837b2f4 add docker file
taiki
parents:
diff changeset
119 regex_t *pattern = NEW(regex_t);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
120 if (regcomp(pattern, name, 0) != 0) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
121 exit(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
122 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
123
ca114837b2f4 add docker file
taiki
parents:
diff changeset
124 /* Confirm user is in GROUP(999) group */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
125
ca114837b2f4 add docker file
taiki
parents:
diff changeset
126 /*
ca114837b2f4 add docker file
taiki
parents:
diff changeset
127 if ( gid != 999 ) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
128 printf("User Not Authorized! Exiting...\n");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
129 exit(1);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
130 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
131 */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
132
ca114837b2f4 add docker file
taiki
parents:
diff changeset
133 /* Set uid, gid, euid and egid to root */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
134
ca114837b2f4 add docker file
taiki
parents:
diff changeset
135 setegid(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
136 seteuid(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
137 setgid(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
138 setuid(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
139
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
140 if (argc >= 3) {
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
141 if ( strncmp(argv[1], pull_command, 6) == 0 ) {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
142 if (regexec(pattern, argv[2], (size_t) 0, NULL, 0)) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
143 fprintf(stderr, bad_name);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
144 exit(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
145 }
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
146
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
147 if (check_name(argv[2])) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
148 fprintf(stderr, bad_name);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
149 exit(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
150 }
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
151
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
152 char exec[1024];
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
153
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
154 strncpy(exec,"/usr/local/bin/newps.py -c /etc/libvirt/qemu/fedora19.xml -n ", 900);
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
155
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
156 strncat(exec, argv[2],1000);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
157 fprintf(stdout, "excuting %s\n",exec );
ca114837b2f4 add docker file
taiki
parents:
diff changeset
158 system(exec);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
159 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
160 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
161
ca114837b2f4 add docker file
taiki
parents:
diff changeset
162
ca114837b2f4 add docker file
taiki
parents:
diff changeset
163 PSLISTPTR pslist = get_pslist(pattern);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
164
ca114837b2f4 add docker file
taiki
parents:
diff changeset
165 char name_xml[1024];
ca114837b2f4 add docker file
taiki
parents:
diff changeset
166 name_xml[0] = 0;
ca114837b2f4 add docker file
taiki
parents:
diff changeset
167 if (argc>=3) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
168 if ( strncmp(argv[1], "define", 6) == 0 ) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
169 strncpy(name_xml,argv[2],900);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
170 strncat(name_xml,".xml",1000);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
171 } else if (check_pslist_name(pslist, argv[2])==0) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
172 fprintf(stderr, bad_name);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
173 print_pslist(pslist);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
174 exit(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
175 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
176 } else if (argc<2) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
177 print_pslist(pslist);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
178 usage();
ca114837b2f4 add docker file
taiki
parents:
diff changeset
179 exit(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
180 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
181
ca114837b2f4 add docker file
taiki
parents:
diff changeset
182 /* Check argv for proper arguments and run
ca114837b2f4 add docker file
taiki
parents:
diff changeset
183 * the corresponding script, if invoked.
ca114837b2f4 add docker file
taiki
parents:
diff changeset
184 */
ca114837b2f4 add docker file
taiki
parents:
diff changeset
185
ca114837b2f4 add docker file
taiki
parents:
diff changeset
186 if (argv[1]==0 || strncmp(argv[1], "list", 4) == 0 ) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
187 print_pslist(pslist);
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
188 } else if (strncmp(argv[1], run_command, 5) == 0) {
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
189 if (execl(command, command, run_command, argv[2], NULL) < 0) {
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
190 perror("Execl:");
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
191 }
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
192 } else if (strncmp(argv[1], start_command, 5) == 0) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
193 if (execl(command, command, start_command, argv[2], NULL) < 0) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
194 perror("Execl:");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
195 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
196 } else if ( strncmp(argv[1], stop_command, 4) == 0 ) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
197 if (execl(command, command, stop_command, argv[2], NULL) < 0) {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
198 perror("Execl:");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
199 }
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
200 } else if ( strncmp(argv[1], build_command, 8) == 0 ) {
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
201 if (execl(command, command, build_command, argv[2], NULL) < 0) {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
202 perror("Execl:");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
203 }
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
204 } else if (strncmp(argv[1], attach_command, 6) == 0 ) {
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
205 if (execl(command, command, attach_command, name_xml, NULL) < 0) {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
206 perror("Execl:");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
207 }
1
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
208 } else if ( strncmp(argv[1], dettach_command, 8) == 0 ) {
3e0e4fd65313 set may use command.
taiki
parents: 0
diff changeset
209 if (execl(command, command, dettach_command, argv[2], NULL) < 0) {
0
ca114837b2f4 add docker file
taiki
parents:
diff changeset
210 perror("Execl:");
ca114837b2f4 add docker file
taiki
parents:
diff changeset
211 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
212 } else {
ca114837b2f4 add docker file
taiki
parents:
diff changeset
213 usage();
ca114837b2f4 add docker file
taiki
parents:
diff changeset
214 exit(1);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
215 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
216 exit(0);
ca114837b2f4 add docker file
taiki
parents:
diff changeset
217 }
ca114837b2f4 add docker file
taiki
parents:
diff changeset
218
ca114837b2f4 add docker file
taiki
parents:
diff changeset
219 /* end */