xref: /aosp_15_r20/external/libcap-ng/utils/proc-llist.c (revision 8dd5e09d5faf27a871e8654ddaa2d2af7c696578)
1 /*
2 * proc-llist.c - Minimal linked list library
3 * Copyright (c) 2009, 2020 Red Hat Inc.
4 * All Rights Reserved.
5 *
6 * This software may be freely redistributed and/or modified under the
7 * terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
19 * Boston, MA 02110-1335, USA.
20 *
21 * Authors:
22 *   Steve Grubb <[email protected]>
23 */
24 
25 #include "config.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include "proc-llist.h"
29 
list_create(llist * l)30 void list_create(llist *l)
31 {
32 	l->head = NULL;
33 	l->cur = NULL;
34 	l->cnt = 0;
35 }
36 
list_append(llist * l,lnode * node)37 void list_append(llist *l, lnode *node)
38 {
39 	lnode* newnode;
40 
41 	if (node == NULL || l == NULL)
42 		return;
43 
44 	newnode = malloc(sizeof(lnode));
45 	if (newnode == NULL)
46 		return;
47 
48 	newnode->ppid = node->ppid;
49 	newnode->pid = node->pid;
50 	newnode->uid = node->uid;
51 	newnode->inode = node->inode;
52 	// Take custody of the memory
53 	newnode->cmd = node->cmd;
54 	newnode->capabilities = node->capabilities;
55 	newnode->bounds = node->bounds;
56 	newnode->ambient = node->ambient;
57 	newnode->next = NULL;
58 
59 	// if we are at top, fix this up
60 	if (l->head == NULL)
61 		l->head = newnode;
62 	else	// Otherwise add pointer to newnode
63 		l->cur->next = newnode;
64 
65 	// make newnode current
66 	l->cur = newnode;
67 	l->cnt++;
68 }
69 
list_clear(llist * l)70 void list_clear(llist* l)
71 {
72 	lnode* nextnode;
73 	register lnode* cur;
74 
75 	cur = l->head;
76 	while (cur) {
77 		nextnode=cur->next;
78 		free(cur->cmd);
79 		free(cur->capabilities);
80 		free(cur->bounds);
81 		free(cur->ambient);
82 		free(cur);
83 		cur=nextnode;
84 	}
85 	l->head = NULL;
86 	l->cur = NULL;
87 	l->cnt = 0;
88 }
89 
list_find_inode(llist * l,unsigned long i)90 lnode *list_find_inode(llist *l, unsigned long i)
91 {
92         register lnode* cur;
93 
94 	cur = l->head;	/* start at the beginning */
95 	while (cur) {
96 		if (cur->inode == i) {
97 			l->cur = cur;
98 			return cur;
99 		} else
100 			cur = cur->next;
101 	}
102 	return NULL;
103 }
104 
105