~ubuntu-branches/ubuntu/oneiric/gnome-system-monitor/oneiric-proposed

« back to all changes in this revision

Viewing changes to src/procactions.c

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2005-08-26 18:38:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050826183824-zh2978nxikpkfxyd
Tags: upstream-2.8.1
ImportĀ upstreamĀ versionĀ 2.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Procman process actions
 
2
 * Copyright (C) 2001 Kevin Vandersloot
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License
 
6
 * as published by the Free Software Foundation; either version 2
 
7
 * of the License, or (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 
17
 *
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#  include <config.h>
 
22
#endif
 
23
#include <errno.h>
 
24
#include <signal.h>
 
25
#include <sys/time.h>
 
26
#include <sys/resource.h>
 
27
#include "procactions.h"
 
28
#include "procman.h"
 
29
#include "proctable.h"
 
30
#include "procdialogs.h"
 
31
#include "callbacks.h"
 
32
 
 
33
static int nice_value = 0;
 
34
 
 
35
static void
 
36
renice_single_process (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 
37
{
 
38
        ProcData *procdata = data;
 
39
        ProcInfo *info = NULL;
 
40
        gint error;
 
41
        gchar *error_msg;
 
42
        GtkWidget *dialog;
 
43
        
 
44
        gtk_tree_model_get (model, iter, COL_POINTER, &info, -1);
 
45
        g_return_if_fail (info);
 
46
        
 
47
        errno = 0;
 
48
        error = setpriority (PRIO_PROCESS, info->pid, nice_value);
 
49
        if (error == -1)
 
50
        {
 
51
                switch (errno) {
 
52
                        case ESRCH:
 
53
                                error_msg = g_strdup_printf (_("No such process."));
 
54
                                dialog = gtk_message_dialog_new (NULL,
 
55
                                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
 
56
                                                                 GTK_MESSAGE_ERROR,
 
57
                                                                 GTK_BUTTONS_OK,
 
58
                                                                 "%s",
 
59
                                                                 error_msg,
 
60
                                                                 NULL); 
 
61
                                gtk_dialog_run (GTK_DIALOG (dialog));
 
62
                                gtk_widget_destroy (dialog);
 
63
                                g_free (error_msg);
 
64
                                break;
 
65
                        case EPERM:
 
66
                                error_msg = g_strdup_printf (_("Process Name: %s \n\nYou do not have permission to change the priority of this process. You can enter the root password to gain the necessary permission."), info->name);
 
67
                                procdialog_create_root_password_dialog (1, procdata, 
 
68
                                                                        info->pid, nice_value,
 
69
                                                                        error_msg);
 
70
                                g_free (error_msg);
 
71
                                break;
 
72
                        case EACCES:
 
73
                                error_msg = g_strdup_printf (_("Process Name: %s \n\nYou must be root to renice a process lower than 0. You can enter the root password to gain the necessary permission."), info->name);
 
74
                                procdialog_create_root_password_dialog (1, procdata, 
 
75
                                                                        info->pid, nice_value,
 
76
                                                                        error_msg);
 
77
                                g_free (error_msg);
 
78
                                break;
 
79
                        default:
 
80
                                break;
 
81
                }
 
82
        }               
 
83
        
 
84
}
 
85
 
 
86
void
 
87
renice (ProcData *procdata, int pid, int nice)
 
88
{
 
89
        nice_value = nice;
 
90
        
 
91
        /*if (!procdata->selection)
 
92
                return;*/
 
93
                
 
94
        gtk_tree_selection_selected_foreach (procdata->selection, renice_single_process, 
 
95
                                             procdata);
 
96
        proctable_update_all (procdata);
 
97
        
 
98
}
 
99
 
 
100
static void
 
101
kill_single_process (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 
102
{
 
103
        ProcData *procdata = data;
 
104
        ProcInfo *info;
 
105
        int error;
 
106
        GtkWidget *dialog;
 
107
        gchar *error_msg;
 
108
        
 
109
        gtk_tree_model_get (model, iter, COL_POINTER, &info, -1);
 
110
        g_return_if_fail (info);
 
111
                
 
112
        /* Author:  Tige Chastian
 
113
           Date:  8/18/01 
 
114
           Added dialogs for errors on kill.  
 
115
           Added sigterm fail over to sigkill 
 
116
        */
 
117
        error = kill (info->pid, kill_signal);
 
118
        if (error == -1)
 
119
        {
 
120
                switch (errno) {
 
121
                        case ESRCH:
 
122
                                break;
 
123
                        case EPERM:
 
124
                                error_msg = g_strdup_printf (_("Process Name: %s \n\nYou do not have permission to end this process. You can enter the root password to gain the necessary permission."), info->name);
 
125
                                procdialog_create_root_password_dialog (0, procdata, 
 
126
                                                                        info->pid, kill_signal,
 
127
                                                                        error_msg);
 
128
                                g_free (error_msg);
 
129
                                break;  
 
130
                        default: 
 
131
                                error = kill (info->pid, SIGKILL);
 
132
                                if (error == -1)
 
133
                                {
 
134
                                        switch (errno) {
 
135
                                        case ESRCH:
 
136
                                                break;
 
137
                                        default:
 
138
                                                dialog = gtk_message_dialog_new (NULL,
 
139
                                                               GTK_DIALOG_DESTROY_WITH_PARENT,
 
140
                                                               GTK_MESSAGE_ERROR,
 
141
                                                               GTK_BUTTONS_OK,
 
142
                                                               "%s",
 
143
                                                              _("An error occured while killing the process."),
 
144
                                                              NULL); 
 
145
                                                gtk_dialog_run (GTK_DIALOG (dialog));
 
146
                                                gtk_widget_destroy (dialog);
 
147
                                        }
 
148
                                }
 
149
                        
 
150
                        }
 
151
        }
 
152
 
 
153
        
 
154
}
 
155
 
 
156
void
 
157
kill_process (ProcData *procdata, int sig)
 
158
{
 
159
        /* EEEK - ugly hack - make sure the table is not updated as a crash
 
160
        ** occurs if you first kill a process and the tree node is removed while
 
161
        ** still in the foreach function
 
162
        */
 
163
        gtk_timeout_remove (procdata->timeout); 
 
164
        
 
165
        kill_signal = sig;      
 
166
        
 
167
        gtk_tree_selection_selected_foreach (procdata->selection, kill_single_process, 
 
168
                                             procdata);
 
169
        
 
170
        procdata->timeout = gtk_timeout_add (procdata->config.update_interval,
 
171
                                             cb_timeout, procdata);         
 
172
        proctable_update_all (procdata);
 
173
        
 
174
}