pidentd  3.0.19
About: implementation of the RFC1413 identification server (more or less complete rewrite compared to version 2)
  Fossies Dox: pidentd-3.0.19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

kernel.c
Go to the documentation of this file.
1 /*
2 ** kernel.c - The kernel access threads
3 **
4 ** Copyright (c) 1997 Peter Eriksson <pen@lysator.liu.se>
5 **
6 ** This program is free software; you can redistribute it and/or
7 ** modify it as you wish - as long as you don't claim that you wrote
8 ** it.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 */
14 
15 #include "config.h"
16 
17 #include <stdio.h>
18 #include <errno.h>
19 #include <syslog.h>
20 #include <string.h>
21 #include <stdlib.h>
22 
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 
27 #include <sys/types.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 
31 #include "pidentd.h"
32 
33 
35 int kernel_buffers = 16;
37 
38 
41 
42 
43 #ifndef HAVE_THREADS
44 static void *ka_data = NULL;
45 #endif
46 
47 
48 struct kernel *
50 {
51  struct kernel *kp;
52 
53  kp = (struct kernel *) buffer_get(&kbuf_free);
54 
55  avail_init(&kp->av);
56 
57  kp->status = -1;
58  kp->euid = NO_UID;
59  kp->ruid = NO_UID;
60 
61  kp->pid = NO_PID;
62  kp->cmd = NULL;
63  kp->argv = NULL;
64 
65  return kp;
66 }
67 
68 
69 void
70 kernel_free(struct kernel *kp)
71 {
72  s_free(kp->cmd);
73  s_free(kp->argv);
74 
75  buffer_put(&kbuf_free, kp);
76 }
77 
78 
79 static void *
80 kernel_thread(void *vp)
81 {
82  struct kernel *kp;
83  int attempt;
84  char buf1[32];
85 
86 
87  if (debug)
88  fprintf(stderr, "kernel_thread() started\n");
89 
90  while ((kp = (struct kernel *) buffer_get(&kbuf_request)) != NULL)
91  {
92  if (debug)
93  {
94  fprintf(stderr, "remote = %s:%d\n",
95  s_inet_ntox(&kp->remote, buf1, sizeof(buf1)),
96  ntohs(SGPORT(kp->remote)));
97 
98  fprintf(stderr, "local = %s:%d\n",
99  s_inet_ntox(&kp->local, buf1, sizeof(buf1)),
100  ntohs(SGPORT(kp->local)));
101  }
102 
103  attempt = 0;
104  while (attempt++ < kernel_attempts)
105  {
106  kp->status = ka_lookup(vp, kp);
107  if (debug)
108  fprintf(stderr, "ka_lookup(), attempt = %d, status = %d\n",
109  attempt, kp->status);
110  if (kp->status > 0)
111  break;
112 
113  if (attempt > 2 && (attempt & 1) == 1)
114  sleep(1); /* Wait for kernel structures to stabilize */
115  }
116 
117  avail_signal(&kp->av);
118 #ifndef HAVE_THREADS
119  break;
120 #endif
121  }
122 
123  if (debug)
124  fprintf(stderr, "kernel_thread() terminating\n");
125 
126  return NULL;
127 }
128 
129 
130 int
132 {
133  int i;
134  pthread_t tid;
135  struct kernel *kp;
136 
137 
138  /*
139  ** Create the request queue
140  */
142  {
143  syslog(LOG_ERR, "buffer_create(%d) failed: %m", kernel_buffers);
144  return -1;
145  }
146 
147 
148  /*
149  ** Create the free pool of buffers
150  */
151  if (buffer_init(&kbuf_free, kernel_buffers * 2) < 0)
152  {
153  syslog(LOG_ERR, "buffer_create(%d) failed: %m", kernel_buffers * 2);
154  return -1;
155  }
156 
157  for (i = 0; i < kernel_buffers * 2; i++)
158  {
159  kp = s_malloc(sizeof(*kp));
160  buffer_put(&kbuf_free, kp);
161  }
162 
163 
164 #ifdef HAVE_THREADS
165  /*
166  ** Create the kernel accessing thread workpool
167  */
168  for (i = 0; i < kernel_threads; i++)
169  {
170  void *key = NULL;
171 
172  if (ka_open(&key) < 0)
173  return -1;
174 
175  pthread_create(&tid, NULL, &kernel_thread, key);
176  }
177 
178  return 0;
179 #else
180  /*
181  ** Open the kernel devices
182  */
183  return ka_open(&ka_data);
184 #endif
185 }
186 
187 
188 void
189 kernel_query(struct kernel *kp)
190 {
191  buffer_put(&kbuf_request, kp);
192 
193 #ifdef HAVE_THREADS
194  avail_wait(&kp->av);
195 #else
197 #endif
198 }
199 
200 
buffer_get
void * buffer_get(buffer_t *bp)
Definition: buffer.c:62
SGPORT
#define SGPORT(sag)
Definition: sockaddr.h:57
ka_lookup
int ka_lookup(void *vp, struct kernel *kp)
Definition: k_aix42.c:125
s_free
void s_free(void *p)
Definition: safeio.c:153
avail_signal
int avail_signal(avail_t *ap)
Definition: avail.c:32
buffer_t
Definition: buffer.h:18
kernel_query
void kernel_query(struct kernel *kp)
Definition: kernel.c:189
ka_open
int ka_open(void **misc)
Definition: k_aix42.c:87
kernel_free
void kernel_free(struct kernel *kp)
Definition: kernel.c:70
pthread_create
int pthread_create(pthread_t *, pthread_attr_t, void *(*)(void *), void *)
pidentd.h
kernel::remote
struct sockaddr_gen remote
Definition: kernel.h:29
ka_data
static void * ka_data
Definition: kernel.c:44
kernel::local
struct sockaddr_gen local
Definition: kernel.h:28
kernel_threads
int kernel_threads
Definition: kernel.c:34
buffer_init
int buffer_init(buffer_t *bp, int bsize)
Definition: buffer.c:24
NO_UID
#define NO_UID
Definition: pidentd.h:55
s_malloc
void * s_malloc(size_t size)
Definition: safeio.c:132
buffer_put
void buffer_put(buffer_t *bp, void *item)
Definition: buffer.c:43
avail_wait
int avail_wait(avail_t *ap)
Definition: avail.c:48
kernel_alloc
struct kernel * kernel_alloc(void)
Definition: kernel.c:49
kernel::euid
uid_t euid
Definition: kernel.h:35
avail_init
int avail_init(avail_t *ap)
Definition: avail.c:23
debug
int debug
Definition: gen_osinfo.c:23
kernel::ruid
uid_t ruid
Definition: kernel.h:36
kbuf_free
static buffer_t kbuf_free
Definition: kernel.c:40
NO_PID
#define NO_PID
Definition: pidentd.h:52
cma_t_handle
Definition: hp_pthread.h:26
kernel::pid
pid_t pid
Definition: kernel.h:38
kernel::cmd
char * cmd
Definition: kernel.h:39
s_inet_ntox
const char * s_inet_ntox(struct sockaddr_in *ia, char *buf, size_t bufsize)
Definition: support.c:468
kernel::argv
char * argv
Definition: kernel.h:40
kernel::status
int status
Definition: kernel.h:34
kbuf_request
static buffer_t kbuf_request
Definition: kernel.c:39
kernel::av
avail_t av
Definition: kernel.h:32
kernel_thread
static void * kernel_thread(void *vp)
Definition: kernel.c:80
kernel_init
int kernel_init(void)
Definition: kernel.c:131
kernel_attempts
int kernel_attempts
Definition: kernel.c:36
kernel_buffers
int kernel_buffers
Definition: kernel.c:35
kernel
Definition: kernel.h:25