]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_proc.c
55072bf67d8f89e8e1d021c12062e17eab38ce1b
[apple/xnu.git] / bsd / kern / kern_proc.c
1 /*
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
62 */
63 /* HISTORY
64 * 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
65 * Added current_proc_EXTERNAL() function for the use of kernel
66 * lodable modules.
67 *
68 * 05-Jun-95 Mac Gillon (mgillon) at NeXT
69 * New version based on 3.3NS and 4.4
70 */
71
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/proc_internal.h>
77 #include <sys/acct.h>
78 #include <sys/wait.h>
79 #include <sys/file_internal.h>
80 #include <ufs/ufs/quota.h>
81 #include <sys/uio.h>
82 #include <sys/malloc.h>
83 #include <sys/mbuf.h>
84 #include <sys/ioctl.h>
85 #include <sys/tty.h>
86 #include <sys/signalvar.h>
87 #include <sys/syslog.h>
88 #include <sys/kernel_types.h>
89
90 /*
91 * Structure associated with user cacheing.
92 */
93 struct uidinfo {
94 LIST_ENTRY(uidinfo) ui_hash;
95 uid_t ui_uid;
96 long ui_proccnt;
97 };
98 #define UIHASH(uid) (&uihashtbl[(uid) & uihash])
99 LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
100 u_long uihash; /* size of hash table - 1 */
101
102 /*
103 * Other process lists
104 */
105 struct pidhashhead *pidhashtbl;
106 u_long pidhash;
107 struct pgrphashhead *pgrphashtbl;
108 u_long pgrphash;
109 struct proclist allproc;
110 struct proclist zombproc;
111 extern struct tty cons;
112
113 /* Name to give to core files */
114 __private_extern__ char corefilename[MAXPATHLEN+1] = {"/cores/core.%P"};
115
116 static void orphanpg(struct pgrp *pg);
117
118 /*
119 * Initialize global process hashing structures.
120 */
121 void
122 procinit()
123 {
124
125 LIST_INIT(&allproc);
126 LIST_INIT(&zombproc);
127 pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
128 pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
129 uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
130 }
131
132 /*
133 * Change the count associated with number of processes
134 * a given user is using.
135 */
136 int
137 chgproccnt(uid, diff)
138 uid_t uid;
139 int diff;
140 {
141 register struct uidinfo *uip;
142 register struct uihashhead *uipp;
143
144 uipp = UIHASH(uid);
145 for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
146 if (uip->ui_uid == uid)
147 break;
148 if (uip) {
149 uip->ui_proccnt += diff;
150 if (uip->ui_proccnt > 0)
151 return (uip->ui_proccnt);
152 if (uip->ui_proccnt < 0)
153 panic("chgproccnt: procs < 0");
154 LIST_REMOVE(uip, ui_hash);
155 FREE_ZONE(uip, sizeof *uip, M_PROC);
156 return (0);
157 }
158 if (diff <= 0) {
159 if (diff == 0)
160 return(0);
161 panic("chgproccnt: lost user");
162 }
163 MALLOC_ZONE(uip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
164 if (uip == NULL)
165 panic("chgproccnt: M_PROC zone depleted");
166 LIST_INSERT_HEAD(uipp, uip, ui_hash);
167 uip->ui_uid = uid;
168 uip->ui_proccnt = diff;
169 return (diff);
170 }
171
172 /*
173 * Is p an inferior of the current process?
174 */
175 int
176 inferior(p)
177 register struct proc *p;
178 {
179
180 for (; p != current_proc(); p = p->p_pptr)
181 if (p->p_pid == 0)
182 return (0);
183 return (1);
184 }
185 /*
186 * Is p an inferior of t ?
187 */
188 int
189 isinferior(struct proc *p, struct proc *t)
190 {
191
192 /* if p==t they are not inferior */
193 if (p == t)
194 return(0);
195 for (; p != t; p = p->p_pptr)
196 if (p->p_pid == 0)
197 return (0);
198 return (1);
199 }
200
201 int
202 proc_isinferior(int pid1, int pid2)
203 {
204 proc_t p;
205 proc_t t;
206
207 if (((p = pfind(pid1)) != (struct proc *)0 ) && ((t = pfind(pid2)) != (struct proc *)0))
208 return (isinferior(p, t));
209 return(0);
210 }
211
212 proc_t
213 proc_find(int pid)
214 {
215 return(pfind(pid));
216 }
217
218 int
219 proc_rele(__unused proc_t p)
220 {
221 return(0);
222 }
223
224 proc_t
225 proc_self()
226 {
227 return(current_proc());
228 }
229
230 proc_t
231 proc_findref(int pid)
232 {
233 boolean_t funnel_state;
234 proc_t p;
235
236 funnel_state = thread_funnel_set(kernel_flock,TRUE);
237 p = pfind(pid);
238
239 if (p != proc_refinternal(p, 1))
240 p = PROC_NULL;
241
242 thread_funnel_set(kernel_flock, funnel_state);
243 return(p);
244 }
245
246 void
247 proc_dropref(proc_t p)
248 {
249
250 proc_dropinternal(p, 0);
251 }
252
253
254 proc_t
255 proc_refinternal(proc_t p, int funneled)
256 {
257
258 proc_t p1 = p;
259 boolean_t funnel_state = TRUE; /* need to init just to avoid warnings and build failure */
260
261 if (funneled == 0)
262 funnel_state = thread_funnel_set(kernel_flock,TRUE);
263
264 if ((p != PROC_NULL) &&(p->p_stat != SZOMB) && ((p->p_lflag & (P_LREFDRAINWAIT | P_LREFDRAIN | P_LREFDEAD)) == 0))
265 p->p_internalref++;
266 else
267 p1 = PROC_NULL;
268
269 if (funneled == 0)
270 thread_funnel_set(kernel_flock,funnel_state);
271 return(p1);
272 }
273
274 void
275 proc_dropinternal(proc_t p, int funneled)
276 {
277 boolean_t funnel_state = TRUE; /* need to init just to avoid warnings and build failure */
278
279 if (funneled == 0)
280 funnel_state = thread_funnel_set(kernel_flock,TRUE);
281
282 if (p->p_internalref > 0) {
283 p->p_internalref--;
284 if ((p->p_internalref == 0) && ((p->p_lflag & P_LREFDRAINWAIT) == P_LREFDRAINWAIT)) {
285 p->p_lflag &= ~P_LREFDRAINWAIT;
286 wakeup(&p->p_internalref);
287 }
288 } else
289 printf("proc_dropreg -ve ref\n");
290
291 if (funneled == 0)
292 thread_funnel_set(kernel_flock,funnel_state);
293 }
294
295
296 int
297 proc_pid(proc_t p)
298 {
299 return(p->p_pid);
300 }
301
302 int
303 proc_ppid(proc_t p)
304 {
305 if (p->p_pptr != (struct proc *)0)
306 return(p->p_pptr->p_pid);
307 return(0);
308 }
309
310 int
311 proc_selfpid(void)
312 {
313 struct proc *p = current_proc();
314 return(p->p_pid);
315 }
316
317
318 int
319 proc_selfppid(void)
320 {
321 struct proc *p = current_proc();
322 if (p->p_pptr)
323 return(p->p_pptr->p_pid);
324 else
325 return(0);
326 }
327
328 void
329 proc_name(int pid, char * buf, int size)
330 {
331 struct proc *p;
332
333 if ((p = pfind(pid))!= (struct proc *)0) {
334 strncpy(buf, &p->p_comm[0], size);
335 buf[size-1] = 0;
336 }
337 }
338
339 void
340 proc_selfname(char * buf, int size)
341 {
342 struct proc *p;
343
344 if ((p = current_proc())!= (struct proc *)0) {
345 strncpy(buf, &p->p_comm[0], size);
346 buf[size-1] = 0;
347 }
348 }
349
350 void
351 proc_signal(int pid, int signum)
352 {
353 proc_t p;
354
355 if ((p = pfind(pid))!= (struct proc *)0) {
356 psignal(p, signum);
357 }
358 }
359
360 int
361 proc_issignal(int pid, sigset_t mask)
362 {
363 proc_t p;
364
365 if ((p = pfind(pid))!= (struct proc *)0) {
366 return(proc_pendingsignals(p, mask));
367 }
368 return(0);
369 }
370
371 int
372 proc_noremotehang(proc_t p)
373 {
374 int retval = 0;
375
376 if (p)
377 retval = p->p_flag & P_NOREMOTEHANG;
378 return(retval? 1: 0);
379
380 }
381
382 int
383 proc_exiting(proc_t p)
384 {
385 int retval = 0;
386
387 if (p)
388 retval = p->p_flag & P_WEXIT;
389 return(retval? 1: 0);
390 }
391
392
393 int
394 proc_forcequota(proc_t p)
395 {
396 int retval = 0;
397
398 if (p)
399 retval = p->p_flag & P_FORCEQUOTA;
400 return(retval? 1: 0);
401
402 }
403
404 int
405 proc_tbe(proc_t p)
406 {
407 int retval = 0;
408
409 if (p)
410 retval = p->p_flag & P_TBE;
411 return(retval? 1: 0);
412
413 }
414
415 int
416 proc_suser(proc_t p)
417 {
418 return(suser(p->p_ucred, NULL));
419
420 }
421
422 kauth_cred_t
423 proc_ucred(proc_t p)
424 {
425 return(p->p_ucred);
426 }
427
428
429 int
430 proc_is64bit(proc_t p)
431 {
432 return(IS_64BIT_PROCESS(p));
433 }
434
435 /* LP64todo - figure out how to identify 64-bit processes if NULL procp */
436 int
437 IS_64BIT_PROCESS(proc_t p)
438 {
439 if (p && (p->p_flag & P_LP64))
440 return(1);
441 else
442 return(0);
443 }
444
445
446 /*
447 * Locate a process by number
448 */
449 struct proc *
450 pfind(pid)
451 register pid_t pid;
452 {
453 register struct proc *p;
454
455 if (!pid)
456 return (kernproc);
457
458 for (p = PIDHASH(pid)->lh_first; p != 0; p = p->p_hash.le_next)
459 if (p->p_pid == pid)
460 return (p);
461 return (NULL);
462 }
463
464 /*
465 * Locate a zombie by PID
466 */
467 __private_extern__ struct proc *
468 pzfind(pid)
469 register pid_t pid;
470 {
471 register struct proc *p;
472
473 for (p = zombproc.lh_first; p != 0; p = p->p_list.le_next)
474 if (p->p_pid == pid)
475 return (p);
476 return (NULL);
477 }
478
479 /*
480 * Locate a process group by number
481 */
482 struct pgrp *
483 pgfind(pgid)
484 register pid_t pgid;
485 {
486 register struct pgrp *pgrp;
487
488 for (pgrp = PGRPHASH(pgid)->lh_first; pgrp != 0; pgrp = pgrp->pg_hash.le_next)
489 if (pgrp->pg_id == pgid)
490 return (pgrp);
491 return (NULL);
492 }
493
494
495 /*
496 * Move p to a new or existing process group (and session)
497 */
498 int
499 enterpgrp(p, pgid, mksess)
500 register struct proc *p;
501 pid_t pgid;
502 int mksess;
503 {
504 register struct pgrp *pgrp = pgfind(pgid);
505
506 #if DIAGNOSTIC
507 if (pgrp != NULL && mksess) /* firewalls */
508 panic("enterpgrp: setsid into non-empty pgrp");
509 if (SESS_LEADER(p))
510 panic("enterpgrp: session leader attempted setpgrp");
511 #endif
512 if (pgrp == NULL) {
513 pid_t savepid = p->p_pid;
514 struct proc *np;
515 /*
516 * new process group
517 */
518 #if DIAGNOSTIC
519 if (p->p_pid != pgid)
520 panic("enterpgrp: new pgrp and pid != pgid");
521 #endif
522 MALLOC_ZONE(pgrp, struct pgrp *, sizeof(struct pgrp), M_PGRP,
523 M_WAITOK);
524 if (pgrp == NULL)
525 panic("enterpgrp: M_PGRP zone depleted");
526 if ((np = pfind(savepid)) == NULL || np != p) {
527 FREE_ZONE(pgrp, sizeof(struct pgrp), M_PGRP);
528 return (ESRCH);
529 }
530 if (mksess) {
531 register struct session *sess;
532
533 /*
534 * new session
535 */
536 MALLOC_ZONE(sess, struct session *,
537 sizeof(struct session), M_SESSION, M_WAITOK);
538 if (sess == NULL)
539 panic("enterpgrp: M_SESSION zone depleted");
540 sess->s_leader = p;
541 sess->s_sid = p->p_pid;
542 sess->s_count = 1;
543 sess->s_ttyvp = NULL;
544 sess->s_ttyp = NULL;
545 bcopy(p->p_session->s_login, sess->s_login,
546 sizeof(sess->s_login));
547 p->p_flag &= ~P_CONTROLT;
548 pgrp->pg_session = sess;
549 #if DIAGNOSTIC
550 if (p != current_proc())
551 panic("enterpgrp: mksession and p != curproc");
552 #endif
553 } else {
554 pgrp->pg_session = p->p_session;
555 pgrp->pg_session->s_count++;
556 }
557 pgrp->pg_id = pgid;
558 LIST_INIT(&pgrp->pg_members);
559 LIST_INSERT_HEAD(PGRPHASH(pgid), pgrp, pg_hash);
560 pgrp->pg_jobc = 0;
561 } else if (pgrp == p->p_pgrp)
562 return (0);
563
564 /*
565 * Adjust eligibility of affected pgrps to participate in job control.
566 * Increment eligibility counts before decrementing, otherwise we
567 * could reach 0 spuriously during the first call.
568 */
569 fixjobc(p, pgrp, 1);
570 fixjobc(p, p->p_pgrp, 0);
571
572 LIST_REMOVE(p, p_pglist);
573 if (p->p_pgrp->pg_members.lh_first == 0)
574 pgdelete(p->p_pgrp);
575 p->p_pgrp = pgrp;
576 LIST_INSERT_HEAD(&pgrp->pg_members, p, p_pglist);
577 return (0);
578 }
579
580 /*
581 * remove process from process group
582 */
583 int
584 leavepgrp(p)
585 register struct proc *p;
586 {
587
588 LIST_REMOVE(p, p_pglist);
589 if (p->p_pgrp->pg_members.lh_first == 0)
590 pgdelete(p->p_pgrp);
591 p->p_pgrp = 0;
592 return (0);
593 }
594
595 /*
596 * delete a process group
597 */
598 void
599 pgdelete(pgrp)
600 register struct pgrp *pgrp;
601 {
602 struct tty * ttyp;
603
604 ttyp = pgrp->pg_session->s_ttyp;
605 if (ttyp != NULL && pgrp->pg_session->s_ttyp->t_pgrp == pgrp) {
606 pgrp->pg_session->s_ttyp->t_pgrp = NULL;
607 }
608 LIST_REMOVE(pgrp, pg_hash);
609 if (--pgrp->pg_session->s_count == 0) {
610 if (ttyp != NULL && (ttyp->t_session == pgrp->pg_session))
611 ttyp->t_session = 0;
612 FREE_ZONE(pgrp->pg_session, sizeof(struct session), M_SESSION);
613 }
614 FREE_ZONE(pgrp, sizeof *pgrp, M_PGRP);
615 }
616
617 void
618 sessrele(sess)
619 struct session *sess;
620 {
621 if (--sess->s_count == 0)
622 FREE_ZONE(sess, sizeof (struct session), M_SESSION);
623 }
624
625 /*
626 * Adjust pgrp jobc counters when specified process changes process group.
627 * We count the number of processes in each process group that "qualify"
628 * the group for terminal job control (those with a parent in a different
629 * process group of the same session). If that count reaches zero, the
630 * process group becomes orphaned. Check both the specified process'
631 * process group and that of its children.
632 * entering == 0 => p is leaving specified group.
633 * entering == 1 => p is entering specified group.
634 */
635 void
636 fixjobc(struct proc *p, struct pgrp *pgrp, int entering)
637 {
638 register struct pgrp *hispgrp;
639 register struct session *mysession = pgrp->pg_session;
640
641 /*
642 * Check p's parent to see whether p qualifies its own process
643 * group; if so, adjust count for p's process group.
644 */
645 if ((hispgrp = p->p_pptr->p_pgrp) != pgrp &&
646 hispgrp->pg_session == mysession) {
647 if (entering)
648 pgrp->pg_jobc++;
649 else if (--pgrp->pg_jobc == 0)
650 orphanpg(pgrp);
651 }
652
653 /*
654 * Check this process' children to see whether they qualify
655 * their process groups; if so, adjust counts for children's
656 * process groups.
657 */
658 for (p = p->p_children.lh_first; p != 0; p = p->p_sibling.le_next)
659 if ((hispgrp = p->p_pgrp) != pgrp &&
660 hispgrp->pg_session == mysession &&
661 p->p_stat != SZOMB) {
662 if (entering)
663 hispgrp->pg_jobc++;
664 else if (--hispgrp->pg_jobc == 0)
665 orphanpg(hispgrp);
666 }
667 }
668
669 /*
670 * A process group has become orphaned;
671 * if there are any stopped processes in the group,
672 * hang-up all process in that group.
673 */
674 static void
675 orphanpg(struct pgrp *pg)
676 {
677 register struct proc *p;
678
679 for (p = pg->pg_members.lh_first; p != 0; p = p->p_pglist.le_next) {
680 if (p->p_stat == SSTOP) {
681 for (p = pg->pg_members.lh_first; p != 0;
682 p = p->p_pglist.le_next) {
683 pt_setrunnable(p);
684 psignal(p, SIGHUP);
685 psignal(p, SIGCONT);
686 }
687 return;
688 }
689 }
690 }
691
692 #ifdef DEBUG
693 void pgrpdump(void); /* forward declare here (called from debugger) */
694
695 void
696 pgrpdump(void)
697 {
698 struct pgrp *pgrp;
699 struct proc *p;
700 u_long i;
701
702 for (i = 0; i <= pgrphash; i++) {
703 if ((pgrp = pgrphashtbl[i].lh_first) != NULL) {
704 printf("\tindx %d\n", i);
705 for (; pgrp != 0; pgrp = pgrp->pg_hash.le_next) {
706 printf("\tpgrp 0x%08x, pgid %d, sess %p, sesscnt %d, mem %p\n",
707 pgrp, pgrp->pg_id, pgrp->pg_session,
708 pgrp->pg_session->s_count,
709 pgrp->pg_members.lh_first);
710 for (p = pgrp->pg_members.lh_first; p != 0;
711 p = p->p_pglist.le_next) {
712 printf("\t\tpid %d addr 0x%08x pgrp 0x%08x\n",
713 p->p_pid, p, p->p_pgrp);
714 }
715 }
716 }
717 }
718 }
719 #endif /* DEBUG */
720
721 /* XXX should be __private_extern__ */
722 int
723 proc_is_classic(struct proc *p)
724 {
725 return (p->p_flag & P_CLASSIC) ? 1 : 0;
726 }
727
728 /* XXX Why does this function exist? Need to kill it off... */
729 struct proc *
730 current_proc_EXTERNAL(void)
731 {
732 return (current_proc());
733 }
734
735 /*
736 * proc_core_name(name, uid, pid)
737 * Expand the name described in corefilename, using name, uid, and pid.
738 * corefilename is a printf-like string, with three format specifiers:
739 * %N name of process ("name")
740 * %P process id (pid)
741 * %U user id (uid)
742 * For example, "%N.core" is the default; they can be disabled completely
743 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
744 * This is controlled by the sysctl variable kern.corefile (see above).
745 */
746 __private_extern__ char *
747 proc_core_name(const char *name, uid_t uid, pid_t pid)
748 {
749 const char *format, *appendstr;
750 char *temp;
751 char id_buf[11]; /* Buffer for pid/uid -- max 4B */
752 size_t i, l, n;
753
754 format = corefilename;
755 MALLOC(temp, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
756 if (temp == NULL)
757 return (NULL);
758 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
759 switch (format[i]) {
760 case '%': /* Format character */
761 i++;
762 switch (format[i]) {
763 case '%':
764 appendstr = "%";
765 break;
766 case 'N': /* process name */
767 appendstr = name;
768 break;
769 case 'P': /* process id */
770 sprintf(id_buf, "%u", pid);
771 appendstr = id_buf;
772 break;
773 case 'U': /* user id */
774 sprintf(id_buf, "%u", uid);
775 appendstr = id_buf;
776 break;
777 default:
778 appendstr = "";
779 log(LOG_ERR,
780 "Unknown format character %c in `%s'\n",
781 format[i], format);
782 }
783 l = strlen(appendstr);
784 if ((n + l) >= MAXPATHLEN)
785 goto toolong;
786 bcopy(appendstr, temp + n, l);
787 n += l;
788 break;
789 default:
790 temp[n++] = format[i];
791 }
792 }
793 if (format[i] != '\0')
794 goto toolong;
795 return (temp);
796 toolong:
797 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
798 (long)pid, name, (u_long)uid);
799 FREE(temp, M_TEMP);
800 return (NULL);
801 }