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