source: ocsinventory-agent/trunk/fuentes/tools/macosx/ocsng_app-xcode/GetPID.h @ 468

Last change on this file since 468 was 468, checked in by mabarracus, 4 years ago

Copyt trusty code

  • Property svn:executable set to *
File size: 8.4 KB
Line 
1/*
2        File:           GetPID.h
3       
4        Description:    This file defines a simple API to do process PID lookup based on process name.
5       
6        Author:         Chad Jones
7
8        Copyright:      © Copyright 2003 Apple Computer, Inc. All rights reserved.
9       
10        Disclaimer:     IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
11                                ("Apple") in consideration of your agreement to the following terms, and your
12                                use, installation, modification or redistribution of this Apple software
13                                constitutes acceptance of these terms.  If you do not agree with these terms,
14                                please do not use, install, modify or redistribute this Apple software.
15
16                                In consideration of your agreement to abide by the following terms, and subject
17                                to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
18                                copyrights in this original Apple software (the "Apple Software"), to use,
19                                reproduce, modify and redistribute the Apple Software, with or without
20                                modifications, in source and/or binary forms; provided that if you redistribute
21                                the Apple Software in its entirety and without modifications, you must retain
22                                this notice and the following text and disclaimers in all such redistributions of
23                                the Apple Software.  Neither the name, trademarks, service marks or logos of
24                                Apple Computer, Inc. may be used to endorse or promote products derived from the
25                                Apple Software without specific prior written permission from Apple.  Except as
26                                expressly stated in this notice, no other rights or licenses, express or implied,
27                                are granted by Apple herein, including but not limited to any patent rights that
28                                may be infringed by your derivative works or by other works in which the Apple
29                                Software may be incorporated.
30
31                                The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
32                                WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
33                                WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34                                PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
35                                COMBINATION WITH YOUR PRODUCTS.
36
37                                IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
38                                CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39                                GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40                                ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
41                                OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
42                                (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
43                                ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44                               
45        Change History (most recent first):
46*/
47#if !defined(__DTSSampleCode_GetPID__)
48#define __DTSSampleCode_GetPID__ 1
49
50#include <stdlib.h>
51#include <stdio.h>
52
53#if defined(__cplusplus)
54extern "C" {
55#endif
56   
57// --- Defining constants for use with sample code --- //
58enum    {kSuccess = 0,
59        kCouldNotFindRequestedProcess = -1, 
60        kInvalidArgumentsError = -2,
61        kErrorGettingSizeOfBufferRequired = -3,
62        kUnableToAllocateMemoryForBuffer = -4,
63        kPIDBufferOverrunError = -5};
64
65/*****************************************************
66 * GetAllPIDsForProcessName
67 *****************************************************
68 * Purpose:  This functions purpose is to lookup a BSD
69 * process PID given the BSD process name.  This function may
70 * potentially return multiple PIDs for a given BSD process name
71 * since several processes can have the same BSD process name.
72 *
73 * Parameters:
74 *      ProcessName             A constant C-string.  On calling
75 * GetAllPIDsForProcessName this variable holds the BSD process name
76 * used to do the process lookup.  Note that the process name you need
77 * to pass is the name of the BSD executable process.  If trying
78 * to find the PID of an regular OSX application you will need to pass the
79 * name of the actual BSD executable inside an application bundle (rather
80 * than the bundle name itself).  In any case as a user you can find the
81 * BSD process name of any process (including OSX applications) by
82 * typing the command "ps -axcocommand,pid" in terminal.
83 *
84 *      ArrayOfReturnedPIDs     A pointer to a pre-allocated array of pid_t.
85 * On calling GetAllPIDsForProcessName this variable must be a pointer to a
86 * pre-allocated array of pid_t whos length (in number of pid_t entries) is defined
87 * in ArrayOfPIDsLength.  On successful return from GetAllPIDsForProcessName
88 * this array will hold the PIDs of all processes which have a matching process
89 * name to that specified in the ProcessName input variable.  The number of actual
90 * PIDs entered in the array starting at index zero will be the value returned
91 * in NumberOfMatchesFound.  On failed return if the error is a buffer overflow
92 * error then the buffer will be filled to the  max with PIDs which matched.
93 * Otherwise on failed return the state of the array will be undefined.
94 *
95 *      NumberOfPossiblePIDsInArray     A unsigned integer.  On calling
96 * GetAllPIDsForProcessName this variable will hold the number of
97 * pre-allocated PID entries which are in the ArrayOfReturnedPIDs for this functions
98 * use.  Note this value must have a value greater than zero.
99 *
100 *      NumberOfMatchesFound    An unsigned integer.  On calling GetAllPIDsForProcessName
101 * this variable will point to a pre-allocated unsigned integer.  On return from
102 * GetAllPIDsForProcessName this variable will contain the number of PIDs contained in the
103 * ArrayOfReturnedPIDs.  On failed return the value of the variable will be undefined.
104 *
105 *      SysctlError     A pointer to a pre-allocated integer.  On failed return, this
106 * variable represents the error returned from the sysctl command.  On function
107 * success this variable will have a value specified by the sysctl based on the
108 * error that occurred.  On success the variable will have the value zero.
109 * Note this variable can also be NULL in which case the variable is ignored.
110 *
111 *      *Function Result*       A integer return value.
112 *                              See result codes listed below.
113 *              Result Codes:
114 *                0             Success.  A set of process PIDs were found and are located in
115 *                              ArrayOfReturnedPIDs array.
116 *               -1             Could not find a process with a matching process name
117 *                              (i.e. process not found).
118 *               -2             Invalid arguments passed.
119 *               -3             Unable to get the size of sysctl buffer required
120 *                              (consult SysctlError return value for more information)
121 *               -4             Unable to allocate memory to store BSD process information
122 *                              (consult SysctlError return value for more information)
123 *               -5             The array passed to hold the returned PIDs is not large enough
124 *                              to hold all PIDs of process with matching names.
125 *
126 *****************************************************/
127int GetAllPIDsForProcessName(const char* ProcessName,
128                             pid_t ArrayOfReturnedPIDs[],
129                             const unsigned int NumberOfPossiblePIDsInArray,
130                             unsigned int* NumberOfMatchesFound,
131                             int* SysctlError); //Can be NULL
132
133/*****************************************************
134 * GetPIDForProcessName
135 *****************************************************
136 * Purpose:  A convience call for GetAllPIDsForProcessName().
137 * This function looks up a process PID given a BSD process
138 * name. 
139 *
140 * Parameters:
141 *      ProcessName             A constant C-string.  On calling
142 * GetPIDForProcessName this variable holds the BSD process name
143 * used to do the process lookup.  Note that the process name you need
144 * to pass is the name of the BSD executable process.  If trying
145 * to find the PID of an regular OSX application you will need to pass the
146 * name of the actual BSD executable inside an application bundle (rather
147 * than the bundle name itself).  In any case as a user you can find the
148 * BSD process name of any process (including OSX applications) by
149 * typing the command "ps -axcocommand,pid" in terminal.
150 *
151 *      *Function Result*       A integer return value.
152 *                              See result codes listed below.
153 *              Result Codes:
154 *                >=0           Success.  The value returned is the PID of the
155 *                              requested process.
156 *               -1             Error getting PID for requested process.  This error can
157 *                              be caused by several things.  One is if no such process exists.
158 *                              Another is if more than one process has the given name.  The
159 *                              Answer is to call GetAllPIDsForProcessName()
160 *                              for complete error code or to get PIDs if there are multiple
161 *                              processes with that name.
162 *****************************************************/
163int GetPIDForProcessName(const char* ProcessName);
164
165#if defined(__cplusplus)
166}
167#endif
168
169#endif
Note: See TracBrowser for help on using the repository browser.