1/*
2 * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * 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; if not, write the Free Software Foundation,
16 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19/*
20* Description:
21* Write the current process in argv[1]
22* and returns:
23*	-> 2 if fork fails with EAGAIN,
24*	-> 1 if there is another problem
25*	-> 0 if everything worked
26*/
27
28#include <sys/types.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <errno.h>
32
33int main(int argc, char **argv)
34{
35	FILE *f;
36	int newpid;
37
38	if (argc != 2) {
39		fprintf(stderr, "Usage: %s /cgroup/.../tasks\n", argv[0]);
40		return 1;
41	}
42
43	f = fopen(argv[1], "a");
44	if (!f) {
45		perror("fopen failed");
46		return 1;
47	}
48
49	fprintf(f, "%i\n", getpid());
50	fclose(f);
51
52	newpid = fork();
53	if (newpid == -1 && errno == EAGAIN)
54		return 2;
55	if (newpid == -1) {
56		perror("fork() failed");
57		return 1;
58	}
59	if (newpid == 0)
60		pause();
61	return 0;
62}
63